1、一般的http post 支持提交鍵值對和二進制數據,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是一個鍵值對的數組,那么抓包的結果都是: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--就算是設置了Content-Type 也是同樣的結果,就是curl默認是以二進制數據的方式提交給服務器的和我們平常的表單提交并不一致服務端如果使用鍵值對的方式獲取就有可能取不到不,所以這里$data 應該使用http_build_query($data) 編碼一次。
2、表單正確的提交方式醫改如下代碼:
$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到服務器端的數據就是:POST /IG_ePolicy/EncryptPassword HTTP/1.1Host: Accept: */*Content-Length: 17Content-Type: application/x-www-form-urlencodedPassword=password
新聞熱點
疑難解答
圖片精選