php curl中x-www-form-urlencoded与multipart/form-data 方式 Post 提交数据详解
multipart/form-data 方式
post的curl庫,模擬post提交的時(shí)候,默認(rèn)的方式 multipart/form-data ,這個算是post提交的幾個基礎(chǔ)的實(shí)現(xiàn)方式。
$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方式
php的curl庫進(jìn)行post提交還是蠻方便的。但是提交方式不同,contentType 不同導(dǎo)致你的api是否能接收到數(shù)據(jù)也是個變數(shù),這里來個簡單的實(shí)例。
$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);
關(guān)鍵一段代碼是
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
以上是云棲社區(qū)小編為您精心準(zhǔn)備的的內(nèi)容,在云棲社區(qū)的博客、問答、公眾號、人物、課程等欄目也有的相關(guān)內(nèi)容,歡迎繼續(xù)使用右上角搜索按鈕進(jìn)行搜索windows , 數(shù)據(jù) , array opera curlformadd 參數(shù)詳解、urlencodedformentity、xwwwform urlencoded、formurlencoded、wwwform urlencoded,以便于您獲取更多的相關(guān)知識。
總結(jié)
以上是生活随笔為你收集整理的php curl中x-www-form-urlencoded与multipart/form-data 方式 Post 提交数据详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三次学JAVA再学不好就吃翔(part
- 下一篇: 深度学习之文本分类模型-基于CNNs系列