6

I am having a problem parsing 'jsonp' request with php's json_decode function.

My questions is

a. What is the use of call back function in 'jsonp', should i just trip that off, or am I suppose to use it in some manner. ?

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

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

1. I request a sample url with PHP's curl

$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 json_decode the received output, which throws the error no 4 meaning JSON_SYNTAX_ERROR, the reason I guess is because names of string type 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' output 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"
         }
      }
   ]
});
Abhishek Madhani
  • 1,155
  • 4
  • 16
  • 26
  • 1
    Check out this answer: http://stackoverflow.com/a/5081588/1788516 – Perry Jul 12 '13 at 10:50
  • @Perry, thanks I did saw that answer, but is there any reason why the responding server sends the callback function. That callback function must be serving some purpose right ? – Abhishek Madhani Jul 12 '13 at 10:52

3 Answers3

2

Callback function is for JS calls - it allows to use API's in AJAX manner, without taking care of same origin policy. When JSONP call is used in JS - browser just calls the callback function that needs to be defined on API client's side.

When you use JSONP inside PHP callback function is not needed at all. If server supports raw JSON type calls - use it, if not strip the callback function strings, in your case

$jsonData = json_decode(substr($feed, 22, -2));
Artek Wisniewski
  • 797
  • 5
  • 13
2

What is the use of call back function in 'jsonp', should i just trip that off, or am I suppose to use it in some manner. ?

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 strip it off. You also need to strip off the ); at the end.

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

You need to fix the data so it really is JSON. The data you have is a JavaScript literal, but it doesn't conform to the subset of JavaScript that matches JSON (e.g. property names are not strings but must be).

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

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Not sure about it but i think names should also be quoted like this:

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: Probably "Categories" too :?

Hristo Valkanov
  • 1,689
  • 22
  • 33
  • I thought that would be taken care by the responding server, unfortunately its not. Is there a way that it can be managed with some php function. – Abhishek Madhani Jul 12 '13 at 10:50
  • Well... the raw data comes as string, seems wrong but u can edit it like any other string. (there should be a better way to do this) You can try reading it line by line exploding it on ":" and setting the first element to have quotes and rewriting it into another string, which will be valid JSON... – Hristo Valkanov Jul 12 '13 at 10:58
  • thank will try that as last resort, as that be tedious on server to process everytime user request a page. – Abhishek Madhani Jul 12 '13 at 11:00