PHP: Handling 'JSONP' output vs 'JSON', and its parsing?

爷,独闯天下 提交于 2019-12-01 17:38:32

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.

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));

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 :?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!