码迷,mamicode.com
首页 > Web开发 > 详细

php curl请求

时间:2018-09-10 11:12:17      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:header   提交   spm   als   数据   field   2.3   2.4   oca   

multipart/form-data 方式

post的curl库,模拟post提交的时候,默认的方式 multipart/form-data ,这个算是post提交的几个基础的实现方式。

$postUrl = ‘‘;
$postData = array(
    ‘user_name‘=>$userName,
    ‘identity_no‘=>$idCardNo
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,‘Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15‘);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);

print_r($r);

 

x-www-form-urlencoded方式

contentType 不同导致你的api是否能接收到数据也是个变数。

$postUrl = ‘‘;
$postData = array(
    ‘user_name‘=>$userName,
    ‘identity_no‘=>$idCardNo
);
$postData = http_build_query($postData);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_USERAGENT,‘Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15‘);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop verifying certificate
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(‘Content-Type: application/x-www-form-urlencoded‘));
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$r = curl_exec($curl);
curl_close($curl);

print_r($r);

 

原文地址:https://yq.aliyun.com/ziliao/604

php curl请求

标签:header   提交   spm   als   数据   field   2.3   2.4   oca   

原文地址:https://www.cnblogs.com/xdtx/p/9617210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!