How do I call a JSON web service from PHP with parameters and callback? How do I call a JSON web service from PHP with parameters and callback? curl curl

How do I call a JSON web service from PHP with parameters and callback?


Try changing your code to this:

$params = array(  'field1' => 'value1',  'field2' => 'value2', 'field3'=> 'value3');$data_string = implode('&',$params);//NB: you may need to urlencode the each of your params$ch = curl_init('https://service:xxx@xxx.xxx.yyyyy.com.xx/service/search?  callback=?&' .$data_string);                                                                      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    $result2 = curl_exec($ch);

Untested code, hope it helps.


The jquery request is using GET. The curl code you wrote appears to be sending a post request (i'm not an expert in curl). Obviously the receiving server handles different types of requests differently, so make sure you send a get via curl and that should help.


getJSON sends a GET request, so you need to convert params array to a string with http_build_query and append it to query. As you're requesting the data with HTTPS you need to point CURL to valid cerficate with CURLOPT_CAINFO / CURLOPT_CAPATH, i'll just ignore the validation in the code.

$params = array(  'field1' => 'value1',  'field2' => 'value2', 'field3'=> 'value3');$url = 'https://service:xxx@xxx.xxx.yyyyy.com.xx/service/search?callback=?' . http_build_query($params);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);$result = curl_exec($ch);if($result === FALSE) {    echo curl_error($ch);}