求这段代码的详解

r_yuan 2010-07-20 04:43:33

<?php
$host = "www.123cha.com";
$referer = "http://".$host;
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
echo "$errstr ($errno)<br>;n";
}else{
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";


fputs ($fp, "$request");
while (!feof($fp))
{
$res[] = fgets($fp,1024);
}
$html = join("",$res);
fclose ($fp);
$fp = file_put_contents("123cha.html",$html);
echo "done";
}


谁能告诉我这段代码每步都是什么意思?谁能写下注释啊,小弟看不懂,正在研究PHP模拟浏览器请求就查到此代码
...全文
135 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
有位高人 学习 HTTP报头
r_yuan 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xuzuning 的回复:]
$url = "http://zh.wikipedia.org/zh-cn/Special:%CB%E6%BB%FA%D2%B3%C3%E6:8080";
print_r(get_headers($url));

这是该url返回的所有标头
Array
(
[0] => HTTP/1.0 403 Forbidden
[1] => Date: Tue, 20 Jul 2010 ……
[/Quote]
什么意思呢
xuzuning 2010-07-20
  • 打赏
  • 举报
回复
$url = "http://zh.wikipedia.org/zh-cn/Special:%CB%E6%BB%FA%D2%B3%C3%E6:8080";
print_r(get_headers($url));

这是该url返回的所有标头
Array
(
[0] => HTTP/1.0 403 Forbidden
[1] => Date: Tue, 20 Jul 2010 09:59:59 GMT
[2] => Server: Apache
[3] => Cache-Control: no-cache
[4] => Vary: Accept-Encoding
[5] => Content-Length: 120
[6] => Content-Type: text/html
[7] => X-Cache: MISS from sq63.wikimedia.org
[8] => X-Cache-Lookup: MISS from sq63.wikimedia.org:3128
[9] => X-Cache: MISS from sq60.wikimedia.org
[10] => X-Cache-Lookup: MISS from sq60.wikimedia.org:80
[11] => Connection: close
)
r_yuan 2010-07-20
  • 打赏
  • 举报
回复

function returnUrl($host)//$host 这里传进来的是这个网址http://zh.wikipedia.org/zh-cn/Special:随机页面
{

$referer = "http://".$host; //定义变量,并连接上一个串
$fp = fsockopen ($host, 8080, $errno, $errstr, 30); // sock 到指定地址(即上面那个)。
if (!$fp){
echo "$errstr ($errno)<br>;n"; //返回空,输出信息
}else{
$request = "
GET / HTTP/1.1
Referer: http://$host
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";

//构造一个get
fputs ($fp, "$request");//向server  发送get请求。
while (!feof($fp))
{
$res[] = fgets($fp,1024); //记录返回的信息。
}
$html = join("",$res);//将返回数组连成一个串。
fclose ($fp); //关闭 socket
$fp = file_put_contents("url.html",$html); //保存串。
return $fp;

}
}


看上面的代码哪里有问题 我还是报一下错误Warning: fsockopen() [function.fsockopen]: unable to connect to http://zh.wikipedia.org/zh-cn/Special:随机页面:8080 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in D:\WebSite\htdocs\Aih\Browser.class.php on line 8
Unable to find the socket transport "http" - did you forget to enable it when you configured PHP? (32)
;n
r_yuan 2010-07-20
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xuzuning 的回复:]
我来解释这段:
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/……
[/Quote]
这条是不是可以接收到新的地址?还有个问题上面的比如Appect我可以不写么?我只想获取到跳转后的地址该怎么写
kyzy_yy_pm 2010-07-20
  • 打赏
  • 举报
回复

;extension=php_sockets.dll前的‘;’去掉
kyzy_yy_pm 2010-07-20
  • 打赏
  • 举报
回复
php.ini中的fsockopen对应的.dll没开启
r_yuan 2010-07-20
  • 打赏
  • 举报
回复

Warning: fsockopen() [function.fsockopen]: unable to connect to http://zh.wikipedia.org/zh-cn/Special:%CB%E6%BB%FA%D2%B3%C3%E6:8080 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) in D:\WebSite\htdocs\Aih\Browser.class.php on line 8
Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?


我用了以上写法 报了这个错误该怎么办
xuzuning 2010-07-20
  • 打赏
  • 举报
回复
我来解释这段:
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."\r\n\r\n";

GET / HTTP/1.1
get方式,遵循 http 1.1 协议

Accept: 。。。
允许列表中的数据类型被返回

Referer: http://$host
模拟来路,以免访问被拒绝

Accept-Language: zh-cn
使用的语言

Accept-Encoding: gzip, deflate
支持压缩方式的返回数据。其实这条不应该写,如果对方真的以压缩方式返回数据的话,不是还有个解压缩的麻烦吗?

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
把自己收成是ie6浏览器

Host: $host
目标服务器地址

Connection: Close"
通知服务器,回传数据后关闭连接
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 kyzy_yy_pm 的回复:]
PHP code

$host = "www.123cha.com"; //网址
$referer = "http://".$host; //浏览器返回(跳转)地址
$fp = fsockopen ($host, 80, $errno, $errstr, 30);//打开一个套接字,网络数据传输
if (!$fp){//如果没有打开
echo "$errst……
[/Quote]

顶^^
jlzan1314 2010-07-20
  • 打赏
  • 举报
回复
模拟http协议:获得数据.
xingworld 2010-07-20
  • 打赏
  • 举报
回复
主要就是获取 123cha.com 的查询结果。然后保存为本地网页文件 123cha.html
CunningBoy 2010-07-20
  • 打赏
  • 举报
回复
<?php
$host = "www.123cha.com";
$referer = "http://".$host;
//打开www.123cha.com的socket连接
$fp = fsockopen ($host, 80, $errno, $errstr, 30);
if (!$fp){
//如果打开失败,输出错误信息
echo "$errstr ($errno)<br>;n";
}else{
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";
// 发送上面的request内容给www.123cha.com
fputs ($fp, "$request");
while (!feof($fp)){
//循环获取网站的返回内容,就是response
$res[] = fgets($fp,1024);
}
//将返回的内容拼接成HTML文档。
$html = join("",$res);
//关闭socket连接
fclose ($fp);
//将网站返回的内容写入文件123cha.html
$fp = file_put_contents("123cha.html",$html);
echo "done";
}
?>
kyzy_yy_pm 2010-07-20
  • 打赏
  • 举报
回复



$host = "www.123cha.com"; //网址
$referer = "http://".$host; //浏览器返回(跳转)地址
$fp = fsockopen ($host, 80, $errno, $errstr, 30);//打开一个套接字,网络数据传输
if (!$fp){//如果没有打开
echo "$errstr ($errno)<br>;n";
}else{//如果打开成功
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";//头信息


fputs ($fp, "$request");//向浏览器写信息(写出后,请求也会随之发送)
while (!feof($fp)){//如果还有需要写的接着写,一直到没有为止
$res[] = fgets($fp,1024);
}
$html = join("",$res);//连接字符串
fclose ($fp);//关闭套接字连接
$fp = file_put_contents("123cha.html",$html);//写入文件
echo "done";
LuciferStar 2010-07-20
  • 打赏
  • 举报
回复
fputs ($fp, $request);
这样就可以了。
床上等您 2010-07-20
  • 打赏
  • 举报
回复


<?php
$host = "www.123cha.com"; //定义变量
$referer = "http://".$host; //定义变量,并连接上一个串
$fp = fsockopen ($host, 80, $errno, $errstr, 30); // sock 到指定地址(即上面那个)。
if (!$fp){
echo "$errstr ($errno)<br>;n"; //返回空,输出信息
}else{
$request = "
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */"."*
Referer: http://$host
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Host: $host
Connection: Close"
."rnrn";

//构造一个get
fputs ($fp, "$request");//向server  发送get请求。
while (!feof($fp))
{
$res[] = fgets($fp,1024); //记录返回的信息。
}
$html = join("",$res);//将返回数组连成一个串。
fclose ($fp); //关闭 socket
$fp = file_put_contents("123cha.html",$html); //保存串。
echo "done"; //告诉你,嘿,完成了
}


21,881

社区成员

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

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