PHPCURL直接访问JSONRPC服务

 1 <?php

 2 $ch = curl_init();

 3 $url = 'http://localhost/jsonrpc?tm='.time().mt_rand (100,999); //参数是为了防止缓存

 4 $options = array(

 5     CURLOPT_URL => $url,

 6     CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",

 7     CURLOPT_RETURNTRANSFER => 1,  

 8     );

 9 curl_setopt_array($ch, $options);

10 #JSON 示例数据

11 curl_setopt($ch, CURLOPT_POSTFIELDS, '{"jsonrpc":"2.0","method":"getOpt","id":1}'); 

12 

13 $html=curl_exec($ch);

14 $info = curl_getinfo($ch);            #获取请求的详细信息 用于调试

15 curl_close($ch); 

16 var_dump($html);

注意CURLOPT_POSTFIELDS是不能http_build_query的,注意这点和普通的POST数据不一样,这里类似文件上传

 

你可能感兴趣的:(json)