用PHP和cURL调用JSON

时间:2022-10-16 20:59:06

I have been trying to call a chinese website API using JSON for over a week now but it is not working. The URL with the parameters I'm supposed to call is:

我一直试图使用JSON调用中文网站API超过一个星期,但它无法正常工作。带有我应该调用的参数的URL是:

http://gw.api.jd.com/routerjson?v=2.0&method=jingdong.ware.product.search.list.get&app_key=XXXXXXXX&360buy_param_json={"isLoadAverageScore":"TRUE","isLoadPromotion":"TRUE","sort":"1","page":"1","pageSize":"10","keyword":"裇衫","client":"apple"}&timestamp=2015-05-07 07:28:14&sign=ZZZZZZZZ

The following code is not working

以下代码无效

$data = array("360buy_param_json" => array("isLoadAverageScore" => "TRUE", "isLoadPromotion" => "TRUE", "sort" => "1", "page" => "1", "pageSize" => "10", "keyword" => "'.$this->searchTerm.'", "client" => "apple") );
$data_string = json_encode($data);

$ch = curl_init('http://gw.api.jd.com/routerjson?v=2.0&method=jingdong.ware.product.search.list.get&app_key=XXXXXXXX&timestamp='.$TimeInChina.'&sign=ZZZZZZZZZ');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);

I've also tried several variations, like moving around the 360buy_param_json field but nothing seems to work.

我也尝试了几种变体,比如在360buy_param_json字段中移动,但似乎没有任何效果。

$data = array("isLoadAverageScore" => "TRUE", "isLoadPromotion" => "TRUE", "sort" => "1", "page" => "1", "pageSize" => "10", "keyword" => "'.$this->searchTerm.'", "client" => "apple") ;
$data_string = '&360buy_param_json='.json_encode($data).'';

Any ideas how to make this puppy work? Thanks Peace

任何想法如何使这只小狗工作?谢谢和平

1 个解决方案

#1


The JSON is supposed to be in the URL, not the post data. And 360buy_param_json is the name of the parameter, not part of the JSON object. Since you're putting it into a URL, you also need to use urlencode to escape it properly.

JSON应该在URL中,而不是在post数据中。 360buy_param_json是参数的名称,不是JSON对象的一部分。由于您将其放入URL,因此您还需要使用urlencode来正确地转义它。

$data = array("isLoadAverageScore" => "TRUE", "isLoadPromotion" => "TRUE", "sort" => "1", "page" => "1", "pageSize" => "10", "keyword" => "'.$this->searchTerm.'", "client" => "apple");
$data_string = urlencode(json_encode($data));
$ch = curl_init('http://gw.api.jd.com/routerjson?v=2.0&method=jingdong.ware.product.search.list.get&app_key=XXXXXXXX&timestamp='.$TimeInChina.'&sign=ZZZZZZZZZ&360buy_param_json='.data_string);

#1


The JSON is supposed to be in the URL, not the post data. And 360buy_param_json is the name of the parameter, not part of the JSON object. Since you're putting it into a URL, you also need to use urlencode to escape it properly.

JSON应该在URL中,而不是在post数据中。 360buy_param_json是参数的名称,不是JSON对象的一部分。由于您将其放入URL,因此您还需要使用urlencode来正确地转义它。

$data = array("isLoadAverageScore" => "TRUE", "isLoadPromotion" => "TRUE", "sort" => "1", "page" => "1", "pageSize" => "10", "keyword" => "'.$this->searchTerm.'", "client" => "apple");
$data_string = urlencode(json_encode($data));
$ch = curl_init('http://gw.api.jd.com/routerjson?v=2.0&method=jingdong.ware.product.search.list.get&app_key=XXXXXXXX&timestamp='.$TimeInChina.'&sign=ZZZZZZZZZ&360buy_param_json='.data_string);