0

When run GET request, the site returns a string instead of JSON with a question mark in the beginning of the string.

?({ "Years": {"min_year":"1941", "max_year":"2017"} });

I tried to use json_encode() function in PHP. However, it returns NULL.

The results is from this site: https://www.carqueryapi.com/api/0.3/?callback=?&cmd=getYears

Does anyone know how to convert the result into JSON? Thanks,

Dale Nguyen
  • 1,930
  • 3
  • 24
  • 37

2 Answers2

5

If you grab the results from the URL without the callback parameter, you can get the standard JSON format.

https://www.carqueryapi.com/api/0.3/?cmd=getYears

{ "Years": {"min_year":"1941", "max_year":"2017"} }

Ben Schroeder
  • 346
  • 2
  • 7
  • Thanks, @Ben. Do you know why the callback function creates such a result? – Dale Nguyen Jun 14 '17 at 19:42
  • 1
    They provided that to implement JSONP which was a workaround for Ajax calls that violate the Same origin Policy. Since you are running server-side php code, it's not an issue for you. – gview Jun 14 '17 at 19:48
2

This is essentially JSONP. They are returning a function ?() with the JSON result inside of it. You would need to remove the outer function, as has been described previously.

They provided that to implement JSONP which was a workaround for Ajax calls that violate the Same origin Policy. This issue and technique is discussed here.

With that said, Ben has found that without the callback param you can avoid the whole issue.

gview
  • 14,876
  • 3
  • 46
  • 51