php curl中x-www-form-urlencoded與multipart/form-data 方式 Post 提交數據詳解

multipart/form-data 方式php

post的curl庫,模擬post提交的時候,默認的方式 multipart/form-data ,這個算是post提交的幾個基礎的實現方式。windows

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

$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);app

print_r($r);curl

想用的能夠直接拿去試試ide

x-www-form-urlencoded方式post

php的curl庫進行post提交仍是蠻方便的。可是提交方式不一樣,contentType 不一樣致使你的api是否能接收到數據也是個變數,這裏來個簡單的實例。ui

$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);url

print_r($r);spa

關鍵一段代碼是

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

以上是雲棲社區小編爲您精心準備的的內容,在雲棲社區的博客、問答、公衆號、人物、課程等欄目也有的相關內容,歡迎繼續使用右上角搜索按鈕進行搜索windows , 數據 , array opera curlformadd 參數詳解、urlencodedformentity、xwwwform urlencoded、formurlencoded、wwwform urlencoded,以便於您獲取更多的相關知識。

相關文章
相關標籤/搜索