1、curl 方法 (真实环境测试,referer content length 等完全正常) 推荐 function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){ $tmpInfo = ''; $cookiepath = getcwd().'./'.$cookiejar; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); if($referer) { curl_setopt($curl, CURLOPT_REFERER, $referer); } else { curl_setopt($curl, CURLOPT_AUTOREFERER, 1); } if($post) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post); } if($cookie) { curl_setopt($curl, CURLOPT_COOKIE, $cookie); } if($cookiejar) { curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiepath); curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiepath); } // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_TIMEOUT, 100); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $tmpInfo = curl_exec($curl); if (curl_errno($curl)) { echo '<pre><b>错误:</b><br />'.curl_error($curl); } curl_close($curl); return $tmpInfo; } 2、sock (空间不支持curl的话,用sock) $data =array(); // 要post的数据 $params=http_build_query($data); $length = strlen($params); // 创建socket连接 $fp = fsockopen("127.0.0.1",80,$errno,$errstr,10) or exit($errstr."--->".$errno); // 构造post请求的头 $header = "POST /mobile/try.php HTTP/1.1/r/n"; // 制定为 POST的方法提交数据 // 及要提交到的页面和协议类型 $header .= "Host:127.0.0.1/r/n"; // 定义主机 $header .= "Referer:http://127.0.0.1/mobile/sendpost.php/r/n"; // Referer信息,如果提交到的地址和当前不在一个域内,并且远程主机起用了防盗链,此值就很重要,必须为远程主机的域名 phperz.com $header .= "Content-Type: application/x-www-form-urlencoded/r/n"; // 说明这个请求为POST $header .= "Content-Length: ".$length."/r/n"; // 提交的数据长度 $header .= "Connection: Close/r/n/r/n";// 关闭连接:注意.此处必须得有四个回车, // 或着在下面post的数据前加上二个加车 /r/n $header .= $params."/r/n";// 添加post的字符串 // 发送post的数据 fputs($fp,$header); $line = fgets($fp,1024); // 去除请求包的头只显示页面的返回数据 while($line) { echo $line; } } fclose($fp); // 关闭socket连接 3、 file_get_contents (以上两种都不支持) function Post($url, $post = null) { $context = array(); if (is_array($post)) { $data=http_build_query($post) ; $context['http'] = array ( 'method' => 'POST', 'header'=> // "Connection: Keep-Alive /r/n ". "Content-type: application/x-www-form-urlencoded /r/n". "Content-length:".strlen($data)."/r/n" . // "Referer: ".$url." /r/n". "User-Agent: ".$_SERVER['HTTP_USER_AGENT']." /r/n/r/n", 'content' => $data, ); } return file_get_contents($url, false, stream_context_create($context)); } 4、骂空间商,要他们curl fsock至少开一个
php 模拟 post curl sock file_get_contents
最新推荐文章于 2019-01-28 13:32:22 发布