1、一般的http post 支持提交鍵值對和二進(jìn)制數(shù)據(jù),win 下使用php 5.6的curl可以這樣模擬
如下代碼:
$ch = curl_init(); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, true); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); //curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-type:application/x-www-form-urlencode")); curl_setopt($ch, CURLOPT_POSTFIELDS, $data);如果data是一個(gè)鍵值對的數(shù)組,那么抓包的結(jié)果都是:POST /IG_ePolicy/EncryptPassWord HTTP/1.1Host: Accept: */*Content-Length: 151Expect: 100-continueContent-Type: application/x-www-form-urlencoded; boundary=------------------------d30a0827949e2a44--------------------------d30a0827949e2a44Content-Disposition: form-data; name="Password"password--------------------------d30a0827949e2a44--就算是設(shè)置了Content-Type 也是同樣的結(jié)果,就是curl默認(rèn)是以二進(jìn)制數(shù)據(jù)的方式提交給服務(wù)器的和我們平常的表單提交并不一致服務(wù)端如果使用鍵值對的方式獲取就有可能取不到不,所以這里$data 應(yīng)該使用http_build_query($data) 編碼一次。
2、表單正確的提交方式醫(yī)改如下代碼:
$data = array("Password"=>"password"); $ch = curl_init(); //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_POST, true); // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); //curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-type:application/x-www-form-urlencode")); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));post到服務(wù)器端的數(shù)據(jù)就是:POST /IG_ePolicy/EncryptPassword HTTP/1.1Host: Accept: */*Content-Length: 17Content-Type: application/x-www-form-urlencodedPassword=password
新聞熱點(diǎn)
疑難解答
網(wǎng)友關(guān)注