2015-05-07 67 views
2

我一直在嘗試使用JSON調用中文網站API一個多星期,但它不工作。 與我應該叫參數的網址是:PHP和cURL的JSON調用

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 

下面的代碼是不工作

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

我也試過幾個變化,像周圍的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).''; 

任何想法如何使這隻小狗工作?謝謝 和平

+1

爲什麼你把JSON放在post字段中,當它應該是一個URL參數? – Barmar

+0

因爲我不知道我在做什麼。非常感謝你! –

回答

1

JSON應該是在URL中,而不是發佈數據。 360buy_param_json是參數的名稱,而不是JSON對象的一部分。由於您將其放入網址中,因此您還需要使用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); 
+0

非常感謝Barmar –