-2

I'm trying to decode this json data by below code, but it's not working.

$data = 'VA_OnQueryData({"name":"John","id":"354902332592"});';
return json_decode($data);
Lynx 242
  • 7,658
  • 5
  • 22
  • 40
Sila Khatun
  • 406
  • 4
  • 13

3 Answers3

10

Get rid of the callback parameter in your URL. Instead of this:

https://api.vinaudit.com/query.php?key=VA_MAIN&callback=VA_OnQueryData&vin=1C6RR6LT3HS847897

Use this:

https://api.vinaudit.com/query.php?key=VA_MAIN&vin=1C6RR6LT3HS847897

Then, you'll get real useful JSON.

The method you were using was JSON-P, and used to be used to get around cross-domain issues by sending up executable JavaScript. This method is no longer required client-side thanks to CORS, and was never required server-side.

Also, go back to whomever makes this API and tell them they're using the wrong Content-Type response header. They're sending text/html... it should be application/json. (Ref: https://stackoverflow.com/a/477819/362536)

Brad
  • 146,404
  • 44
  • 300
  • 476
  • Damn, you beat me to it. – ceejayoz Jul 12 '18 at 19:40
  • 1
    @SilaKhatun If you found this answer useful, you can accept it as the answer to your problem by clicking the check mark next to it. Then, it will bounce to the top for future readers. Thanks. – Brad Jul 12 '18 at 19:46
1

Hi what you are trying to json_decode is not valid json.

If you have to work with it this way, you will need to clean it first as follows:

$data = 'VA_OnQueryData({"vin":"1C6RR6LT3HS847897","id":"697015470432","attributes":{"VIN":"1C6RR6LT3HS847897","Year":"2017","Make":"Ram","Model":"1500","Trim":"Lone Star","Made In":"United States","Style":"Crew Cab Pickup (4-Door)","Engine":"5.7L V8 OHV 16V"},"success":true,"error":""});';

preg_match('/{.*}/', $data, $cleaned);

return json_decode($cleaned[0]);

The output of this is:

stdClass Object
(
    [vin] => 1C6RR6LT3HS847897
    [id] => 697015470432
    [attributes] => stdClass Object
        (
            [VIN] => 1C6RR6LT3HS847897
            [Year] => 2017
            [Make] => Ram
            [Model] => 1500
            [Trim] => Lone Star
            [Made In] => United States
            [Style] => Crew Cab Pickup (4-Door)
            [Engine] => 5.7L V8 OHV 16V
        )

    [success] => 1
    [error] => 
)
Simon K
  • 871
  • 1
  • 6
  • 7
0

It's not working because it is not a json. Try this instead:

$data = '{"name":"John","id":"354902332592"}';
return json_decode($data);
Karol Samborski
  • 2,380
  • 6
  • 17
  • Thanks for your answer, but i have to decode this data $data = 'VA_OnQueryData({"name":"John","id":"354902332592"});'; cause i'm getting in this format from API. – Sila Khatun Jul 12 '18 at 19:36