为什么对于www.xinhuanet.com使用fsockopen它的时候,程序无法返回正确信息?

crodling 2005-12-08 12:05:15

function GetWebContent($host, $method, $str, $sessid = '')
{
$ip = gethostbyname($host);
$fp = fsockopen($ip, 80);
if (!$fp) return;
fputs($fp, "$method\r\n");
fputs($fp, "Host: $host\r\n");
if (!empty($sessid))
{
fputs($fp, "Cookie: PHPSESSID=$sessid; path=/;\r\n");
}
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, "Content-Length: ". strlen($str) . "\r\n"); // 别忘了指定长度
}
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
if ( substr(trim($method),0, 4) == "POST")
{
fputs($fp, $str."\r\n");
}
while(!feof($fp))
{
$response .= fgets($fp, 1024);
}
$hlen = strpos($response,"\r\n\r\n"); // LINUX下是 "\n\n"
$header = substr($response, 0, $hlen);
$entity = substr($response, $hlen + 4);
echo $header;
if ( preg_match('/PHPSESSID=([0-9a-z]+);/i', $header, $matches))
{
$a['sessid'] = $matches[1];
}
if ( preg_match('/Location: ([0-9a-z\_\?\=\&\#\.]+)/i', $header, $matches))
{
$a['location'] = $matches[1];
}
$a['content'] = $entity;
fclose($fp);
return $a;
}
$response = GetWebContent("www.xinhuanet.com","POST / HTTP/1.0", $str);
echo $response['location'].$response['content']."<br>";
echo $response['sessid']."<br>";

代码上,它总返回给我一个空白页面
而我使用其他站点时候,都是可以正常显示网页内容
请问是xinhuanet做了什么限制的缘故么?请高手指点
...全文
278 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhustar 2005-12-27
  • 打赏
  • 举报
回复
用fopen ,自 PHP 4.0.5 起支持重定向。如果使用较早版本的 PHP 则需要在 URL 末尾包括一个斜线。如果一定要知道文档所在的资源的 url(在所有重定向被处理过之后),则需要处理从流返回的一系列响应报头信息。


<?php
$url = 'http://www.example.com/redirecting_page.php';

$fp = fopen($url, 'r');

/* Prior to PHP 4.3.0 use $http_response_header
instead of stream_get_meta_data() */
foreach(stream_get_meta_data($fp) as $response) {

/* Were we redirected? */
if (substr(strtolower($response), 0, 10) == 'location: ') {
/* update $url with where we were redirected to */
$url = substr($response, 10);
}

}

?>


流允许访问资源的正文,报头部分保存在 $http_response_header 变量中。自 PHP 4.3.0 起,可以用 stream_get_meta_data() 得到报头。

或者你把报头里的 Location找出来,再读一次,nettransport就是这样的
crodling 2005-12-09
  • 打赏
  • 举报
回复
我想问一下每做一次fsockopen,,是否就等于打开了一个新的窗口去open这个url导致session发生了变化?
crodling 2005-12-08
  • 打赏
  • 举报
回复
302 Moved Temporarily
通过返回的头可以看出,xinhuanet使用了重定向,关键在于,我如何获取那个重定向后的页面?
这里,我试图把http://202.84.17.89/sso/authenticatet?serverid=publish&localsessionid=ca541159ce83c41b7ce1de84adebf5dfad81ffe6d3e&requesturl=http%3A%2F%2F202.84.17.88%2Feng%2Findex.jsp%3Flocale%3Deng&action=0
这个地址放进去
$response = GetWebContent("202.84.17.89","POST / /sso/authenticatet?serverid=publish&localsessionid=ca541159ce83c41b7ce1de84adebf5dfad81ffe6d3e&requesturl=http%3A%2F%2F202.84.17.88%2Feng%2Findex.jsp%3Flocale%3Deng&action=0
HTTP/1.0", $str);
可是结果却是构造了一串更长的location出来
crodling 2005-12-08
  • 打赏
  • 举报
回复
HTTP/1.1 302 Moved Temporarily Date: Thu, 08 Dec 2005 01:08:46 GMT Server: Oracle-Application-Server-10g/9.0.4.0.0 Oracle-HTTP-Server Set-Cookie: JSESSIONID=ca541159ce83c41b7ce1de84adebf5dfad81ffe6d3e.s6vymBrzmxuIs6zIo7jvpAjO-AbJphCNaN0Ocybtah0Iah0N-x8IahuInh9ymkiT-Anya6aLmA8Qck8I-huKa30xok5Nah1Bq7jwc2TSo6fwrQjwc2TSokTCrk5InleImQXH8N4Pb30Oc3yPbNqxf2bKrk9tnkSxn6jAmljGr5XDqQLvpAe_; Path=/eng Cache-Control: private Location: http://202.84.17.89/sso/authenticatet?serverid=publish&localsessionid=ca541159ce83c41b7ce1de84adebf5dfad81ffe6d3e&requesturl=http%3A%2F%2F202.84.17.88%2Feng%2Findex.jsp%3Flocale%3Deng&action=0 Connection: close Content-Type: text/plain
http

以上便是返回信息

http://202.84.17.89/sso/authenticatet?serverid=publish&localsessionid=ca541159ce83c41b7ce1de84adebf5dfad81ffe6d3e&requesturl=http%3A%2F%2F202.84.17.88%2Feng%2Findex.jsp%3Flocale%3Deng&action=0
对于这个地址我直接放到浏览器里面就可以正常显示
xuzuning 2005-12-08
  • 打赏
  • 举报
回复
不是无法返回正确的信息,如果是的话浏览器也不能访问
而是你不能正确解析返回的信息

贴出返回的信息

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧