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);
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);
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)
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] =>
)
It's not working because it is not a json. Try this instead:
$data = '{"name":"John","id":"354902332592"}';
return json_decode($data);