首页 > 编程 > PHP > 正文

PHP 5.6 的Curl POST

2019-11-06 06:05:36
字体:
来源:转载
供稿:网友

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表