Dale Nguyen
Dale Nguyen

Reputation: 1980

Convert String to Json in PHP from GET Request

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,

Upvotes: 0

Views: 196

Answers (2)

gview
gview

Reputation: 15361

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.

Upvotes: 2

Ben Schroeder
Ben Schroeder

Reputation: 356

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"} }

Upvotes: 5

Related Questions