PHP: Handling JSONP vs. JSON output and parsing it?

I had a problem processing the jsonp request using the php function json_decode .

My questions

a. What is the jsonp callback function if I just turn it off, or I suppose to use it somehow.

b. How can I fix the syntax error received in jsonp format?

Below I have given the code and the answer that I get.

1. I am requesting a url sample with curl PHP

 $url = 'https://ssl.domain.com/data/4564/d.jsonp'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); $feed = curl_exec($ch); curl_close($ch); echo $feed = gzdecode($feed); // Success its displays the jsonp feed 

2. Then I try to execute json_decode the output that produces error 4, which means JSON_SYNTAX_ERROR , the reason, in my opinion, is that the string type names in jsonp are not quoted. e.g. Categories , Name , Position , etc.

 $json_feed = json_decode($feed); $error = json_last_error(); echo $error; // Throws error no. 4 

3. RAW 'jsonp' is inferred from the URL.

 domain_jsonp_callback({ Categories:[ { Name:"Artifacts", Position:14, Count:70, ImageUrls:{ i100:"//s3-eu-west-1.amazonaws.com/s.domain.com/1.png", i120:"//s3-eu-west-1.amazonaws.com/s.domain.com/2.png", i140:"//s3-eu-west-1.amazonaws.com/s.domain.com/3.png", i180:"//s3-eu-west-1.amazonaws.com/s.domain.com/4.png", i220:"//s3-eu-west-1.amazonaws.com/s.domain.com/5.png", i280:"//s3-eu-west-1.amazonaws.com/s.domain.com/6.png" } } ] }); 
+6
source share
3 answers

How to use the callback function in 'jsonp' if I just disable it, or I suppose to use it somehow.

JSON-P is really a JavaScript script that consists of a function call with an argument.

If you want to parse it in PHP, then yes, you need to remove it. You also need to remove from the end ); .

b. How can I fix the syntax error received in jsonp format?

You need to fix the data, so this is really JSON. The existing data is a JavaScript literal, but it does not match a subset of JavaScript that matches JSON (for example, property names are not strings, but should be).

It would be better to get a real JSON resource instead of a source.

+2
source

The callback function is designed for JS calls - this allows you to use the API in AJAX mode without worrying about the same origin policy. When a JSONP call is used in a JS browser, it simply calls the callback function, which must be defined on the client side of the API.

When you use JSONP inside PHP, the callback function is not needed at all. If the server supports raw JSON calls, use it if you don’t fade the lines of the callback function, in your case

 $jsonData = json_decode(substr($feed, 22, -2)); 
+2
source

Not sure, but I think the names should also be listed as follows:

 domain_jsonp_callback({ Categories:[ { "Name":"Artifacts", "Position":14, "Count":70, "ImageUrls":{ "i100":"//s3-eu-west-1.amazonaws.com/s.domain.com/1.png", "i120":"//s3-eu-west-1.amazonaws.com/s.domain.com/2.png", "i140":"//s3-eu-west-1.amazonaws.com/s.domain.com/3.png", "i180":"//s3-eu-west-1.amazonaws.com/s.domain.com/4.png", "i220":"//s3-eu-west-1.amazonaws.com/s.domain.com/5.png", "i280":"//s3-eu-west-1.amazonaws.com/s.domain.com/6.png" } } ] }); 

PS: Perhaps also "Categories" :?

+1
source

Source: https://habr.com/ru/post/949305/


All Articles