2

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"}&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.

$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

2
  • 1
    Why are you putting the JSON in the post fields when it's supposed to be a URL parameter? Commented May 7, 2015 at 0:43
  • Because I had no idea what I was doing. Thank you very much! Commented May 7, 2015 at 1:35

1 Answer 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.

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.