php curl传json

1.封装的curl公共方法
function curlJson( u r l , url, data){ //传json post
c h = c u r l i n i t ( ) ; c u r l s e t o p t ( ch = curl_init(); curl_setopt( ch, CURLOPT_URL, u r l ) ; c u r l s e t o p t ( url); curl_setopt( ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’)); //设置请求头,这个一定要写
curl_setopt( c h , C U R L O P T P O S T , 1 ) ; c u r l s e t o p t ( ch, CURLOPT_POST, 1); curl_setopt( ch, CURLOPT_POSTFIELDS, d a t a ) ; c u r l s e t o p t ( data); curl_setopt( ch, CURLOPT_RETURNTRANSFER, true);
r e s p o n s e = c u r l e x e c ( response = curl_exec( ch);
curl_close($ch);
return $response;
}
在这里插入图片描述

2.调用:
public function dayin(){
$url = ‘http://localhost/xxx/xxx/xxx’; //请求的路径
$string = array( //请求的参数
‘OrderType’=>1,
‘PrintContent’=>1,
‘PrintPaper’=>‘A4’,
‘CorpBillidDatas’=>array(
‘CorpBillid’ => ‘5c3fca6c71ed922e1487311d’
),
‘Verify’=>array(
‘Clientid’ => ‘758’,
‘Token’ => ‘9VdKIr2FB8Jr40XSCBRW’
)
);
p a r a m e = j s o n e n c o d e ( parame = json_encode( string); //参数转成json
v a l = c u r l J s o n ( val = curlJson( url,$parame); //调用方法访问接口
return $val; //输出 =返回值
}
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_39815001/article/details/86595382