如何使用参数和回调从PHP调用JSON Web服务?

时间:2012-11-07 01:16:55

标签: php json web-services curl callback

我从JQuery $ .getJSON函数调用web服务,它工作正常。

    var p = {
       'field1': 'value1',
       'field2': 'value2',
       'field3': 'value3'
    };

    $.getJSON('https://service:xxx@xxx.xxx.yyyyy.com.xx/service/search?callback=?', p, function(data) {
    if (data[0]) {      
        // print results
    } else  {
        // no results found
    }
});

我正在尝试从PHP和CURL连接,但它不起作用,它总是返回false。

//首先尝试

$params = array(  'field1' => 'value1',  'field2' => 'value2', 'field3'=> 'value3');
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, 'https://service:xxx@xxx.xxx.yyyyy.com.xx/service/search?callback=?');
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch); // return false instead of my JSON

// SECOND TRY

    $data_string = json_encode($params);                                                                                   
    $ch = curl_init('https://https://service:xxx@xxx.xxx.yyyyy.com.xx/service/search?callback=?');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

    $result2 = curl_exec($ch); //return false instead of my JSON

我做错了什么?

非常感谢,

3 个答案:

答案 0 :(得分:1)

尝试将代码更改为:

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

未经测试的代码,希望它有所帮助。

答案 1 :(得分:0)

jquery请求正在使用GET。你写的卷曲代码似乎是发送一个帖子请求(我不是卷曲专家)。显然,接收服务器以不同的方式处理不同类型的请求,因此请确保通过curl发送get,这应该会有所帮助。

答案 2 :(得分:0)

getJSON发送GET请求,因此您需要将params数组转换为带有http_build_query的字符串并将其追加到查询中。当您使用HTTPS请求数据时,您需要使用CURLOPT_CAINFO / CURLOPT_CAPATH将CURL指向有效的cerficate,我将忽略代码中的验证。

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