使用fsockopen 打开一个链接时,如果打开的链接返回一个302的重定向到登陆成功页面麻烦就来了,COOKIE可能没问题,但SESSION会导致产生一个新的会话,马上就会失效还是需要登陆的。
解决办法超级简单,把它的返回头一字不落的header出去。
$fp = fsockopen($urlInfo['host'], $port, $errno, $errstr, $timeout) or die('登陆失败:' + $errstr);
$out .= "POST {$urlInfo['path']} HTTP/1.0\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "'Connection: keep-alive\r\n";
$out .= "Content-Length: " . strlen($postStr) . "\r\n\r\n";
$out .= $postStr . "\r\n\r\n";
fwrite($fp, $out);
$xx = array();
while (!feof($fp)) {
$xx[] = fgets($fp);
}
fclose($fp);
if (!empty($xx)) {
foreach ($xx as $v) {
header($v);
}
}