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,

3
  • Because instead of ? should be function name, I suppose. This is called jsonp Commented Jun 14, 2017 at 19:37
  • Would be an encode, not a decode. Commented Jun 14, 2017 at 19:38
  • @ficuscr. Sorry, it's a typo. I used encode. Commented Jun 14, 2017 at 19:41

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, @Ben. Do you know why the callback function creates such a result?
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.
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.

1 Comment

Thanks @gview for the explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.