PHP支付接口对接curl Post方式提交详解

在做支付接口对接的时候,会遇到使用PHP的curl函数实现Get和Post请求,详细说明如下。

$headers = [ "Content-type: application/json;charset='utf-8'", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache" ];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);//当请求https的数据时,会要求证书,这时候,加上以上这两个参数,规避ssl的证书检查
curl_setopt($ch, CURLOPT_TIMEOUT, 60);//设置超时时间
curl_setopt($ch, CURLOPT_POST, TRUE);//设置post方式提交
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post)); //post提交数据,数组格式,键名是name,键值是value
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//CURLOPT_RETURNTRANSFER 不设置 curl_exec返回TRUE 设置 curl_exec返回json(此处) 失败都返回FALSE
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);  
$data = curl_exec($ch); 
curl_close($ch);

猜你喜欢

转载自blog.csdn.net/jeesr/article/details/91445369