PHPCURL直接访问JSONRPC服务

时间:2022-12-28 05:08:00
 <?php
$ch = curl_init();
$url = 'http://localhost/jsonrpc?tm='.time().mt_rand (100,999); //参数是为了防止缓存
$options = array(
CURLOPT_URL => $url,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0",
CURLOPT_RETURNTRANSFER => 1,
);
curl_setopt_array($ch, $options);
#JSON 示例数据
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"jsonrpc":"2.0","method":"getOpt","id":1}'); $html=curl_exec($ch);
$info = curl_getinfo($ch); #获取请求的详细信息 用于调试
curl_close($ch);
var_dump($html);

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