php curl传json

本文介绍了一种使用CURL封装JSON POST请求的公共方法,包括设置请求头、POST数据和接收响应。同时展示了如何调用该方法进行HTTP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.封装的curl公共方法
function curlJson( u r l , url, 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=curlinit();curlsetopt(ch, CURLOPT_URL, u r l ) ; c u r l s e t o p t ( url); curl_setopt( url);curlsetopt(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,CURLOPTPOST,1);curlsetopt(ch, CURLOPT_POSTFIELDS, d a t a ) ; c u r l s e t o p t ( data); curl_setopt( data);curlsetopt(ch, CURLOPT_RETURNTRANSFER, true);
r e s p o n s e = c u r l e x e c ( response = curl_exec( response=curlexec(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( parame=jsonencode(string); //参数转成json
v a l = c u r l J s o n ( val = curlJson( val=curlJson(url,$parame); //调用方法访问接口
return $val; //输出 =返回值
}
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值