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.1
Host:
Accept: */*
Content-Length: 151
Expect: 100-continue
Content-Type: application/x-www-form-urlencoded; boundary=------------------------d30a0827949e2a44
--------------------------d30a0827949e2a44
Content-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.1
Host:
Accept: */*
Content-Length: 17
Content-Type: application/x-www-form-urlencoded
Password=password