PHP curl模拟浏览器网页form表单GET/POST提交数据
开发中经常遇到需要模拟访客通过网页端提交数据的行为,这些其实都能用PHP的CURL来实现
网页POST数据提交的是&串起来的 比如loginname=bnxbcom&password=1234566&token=djeur
$output_array=array(); $postdata='loginname=bnxbcom&password=1234566&token=djeur'; $referer='';$url='';$result=''; $header=array();//如果需要header那就这里设置 $referer='https://www.bnxb.com'; $url='https://www.bnxb.com/login'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); if(isset($header)){ curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } //curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, $referer);//模拟来源页面 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0');//模拟UA if($type=='post'){ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);//代理设置 //curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); //curl_setopt($ch, CURLOPT_PROXY, "123.169.34.11");//代理IP //curl_setopt($ch, CURLOPT_PROXYPORT, "9999");//代理端口 //curl_setopt($ch, CURLOPT_PROXYUSERPWD, "taras:taras-ss5");//代理密码 $result=curl_exec($ch); curl_close($ch);
如果需要提交数组,记得把
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
改成
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata));
这样才能把数组转换成FORM提交的&连接的字串格式
顶(0)
踩(0)
- 最新评论