PHP:处理"JSONP"输出与"JSON"及其解析? [英] PHP: Handling 'JSONP' output vs 'JSON', and its parsing?

查看:722
本文介绍了PHP:处理"JSONP"输出与"JSON"及其解析?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用php的json_decode函数解析"jsonp"请求时遇到问题.

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

我的问题是

a. 'jsonp'中回调函数的用途是什么,我应该将其绊倒,还是我想以某种方式使用它. ?

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. 如何纠正以"jsonp"格式收到的语法错误?

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..我请求一个带有PHP卷曲的示例网址

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..然后我尝试对接收到的输出进行json_decode,这将引发错误4,表示

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'输出.

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"
         }
      }
   ]
});

推荐答案

'jsonp'中回调函数的用途是什么,我应该将其绊倒,还是我想以某种方式使用它. ?

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实际上是一个JavaScript脚本,由带参数的函数调用组成.

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

如果您想用PHP解析它,那么是的,您需要将其剥离.您还需要在最后剥离);.

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.如何纠正以"jsonp"格式收到的语法错误?

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

您需要修复数据,以便它实际上是JSON.您拥有的数据是JavaScript文字,但不符合与JSON匹配的JavaScript子集(例如,属性名称不是字符串,但必须是字符串).

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

最好从源中获取真实的JSON资源.

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

这篇关于PHP:处理"JSONP"输出与"JSON"及其解析?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆