PHP 模拟HTTP发送POST请求
$URL_Info=parse_url($URL);$referrer=$_SERVER["SCRIPT_URI"];foreach($post_dataas$key=>...
$URL_Info=parse_url($URL); $referrer=$_SERVER["SCRIPT_URI"]; foreach ($post_data as $key=>$value){ $values[]="$key=".urlencode($value); } $data_string=implode("&",$values); $URL_Info["port"]=8080; $request.="POST ".$URL_Info["path"]." HTTP/1.1\r\n"; $request.="Host: ".$URL_Info["host"]."\r\n"; $request.="Referer: $referrer\r\n"; $request.="Content-type: application/x-www-form-urlencoded\r\n"; $request.="Content-length: ".strlen($data_string)."\r\n"; $request.="Connection: Keep-Alive\r\n"; $request.="\r\n"; $request.=$data_string."\r\n"; $fp = fsockopen($URL_Info["host"], $URL_Info["port"]); fputs($fp, $request);//send http head while(!feof($fp)) { $result .= fgets($fp, 1024); } $a=strpos($result,'{'); $result = substr($result,$a,strlen($result)); 我这样 Connection: 为Keep-Alive 为什么就不成功 为close就行 还有为close后 为什么求情后不能获得sessionKey是不是 HTTP头还缺少什么 我不懂啊 谁知道帮帮我把
展开
1个回答
展开全部
用php模拟登陆主要分为三部分1. post数据。2.根据返回的http头,从中截出cookie段。3.伪造http头发送请求。 我这里以用php抓取163相册的需要密码才能访问的目录为例。<?phpfunction posttohost($url, $data) //post数据if (!isset($url['query'])) $encoded = "";foreach ($data as $k=>$v) $fp = fsockopen($url['host'], $url['port'] ? $url['port'] : 80);if (!$fp) return "Failed to open socket to $url[host]";fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? "?" : "", $url['query']));fputs($fp, "Host: $url[host]\n");fputs($fp, "Content-type: application/x-www-form-urlencoded\n");fputs($fp, "Content-length: " . strlen($encoded) . "\n");fputs($fp, "Connection: close\n\n");fputs($fp, "$encoded\n");$line = fgets($fp,1024);if (!eregi("^HTTP/1\.. 200", $line)) return;$results = ""; $inheader = 1;while(!feof($fp)) elseif ($inheader) }fclose($fp);return $results;} 答案补充 function getjs($juser,$jaid,$jcookie) //伪造http头 答案补充 else fclose( $socket ); return $ret;}}$iurl=' http://photo.163.com/photos/'.$iuser.'/'.$aid.'/';$idata=array ('pass'=>$pass,'checking'=>'1'); //通过winsock抓包发现,输入访问密码,会向163相册发送 pass=密码&checking=1$mystr=posttohost($iurl,$idata);$pattern='/HALFORDER=(.*?);/';preg_match($pattern,$mystr,$out);$str=getjs($iuser,$aid,$out[1]);echo $str;?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询