Reputation: 48
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:
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"}×tamp=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×tamp='.$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.
$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
Upvotes: 2
Views: 110
Reputation: 780879
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.
$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×tamp='.$TimeInChina.'&sign=ZZZZZZZZZ&360buy_param_json='.data_string);
Upvotes: 1