2009年7月21日星期二

用 PHP 发送跨域 HTTP 请求


<?php
$f = fsockopen("www.google.com"80);
fputs($f, "GET /search?q=PHP+site:m13253.blogspot.com HTTP/1.1\r\n");
fputs($f, "Host: www.google.com\r\n");
fputs($f, "Connection: close\r\n");
fputs($f, "\r\n");
fputs($f, chr(26));
while(!feof($f) && ($s=fgets($f))!="\r\n" && $s!="\n" && $s!="\r");
while(!feof($f))
        echo(fgetc($f));
fclose($f);
?>

这段代码是用PHP连接 http://www.google.com/search?q=PHP+sitein:m13253.blogspot.com
发送这样的请求:

GET /search?q=PHP+sitein:m13253.blogspot.com HTTP/1.1
Host: www.google.com

Connection: close



然后等待服务器返回
如果成功,服务器会返回一个HEADER(依照具体情况而定)


HTTP/1.1 200 OK
Date: *******************
Server: *****************
Content-type: *********
Content-length: *******


再加上一个空行,然后是网页正文。
网络上有些脚本没有过滤HTTP HEADER
但是现在只能获取HTTP 200 OK状态的内容,
无法判断重定向。

建议使用Apache + PHP 5运行。
------------------------------- 我 是 分 隔 线 -------------------------------
第1次修改后的版本:
<?php
define("host""www.google.com");
define("url""/search?q=PHP+site:m13253.blogspot.com");
$f = fsockopen(host80);
if($f)
{
        fputs($f, "GET ".url." HTTP/1.1\r\n");
        fputs($f, "Host: ".host."\r\n");
        fputs($f, "Connection: close\r\n");
        fputs($f, "\r\n");
        while(!feof($f) && ($s=fgets($f))!="\r\n" && $s!="\n" && $s!="\r");
        while(!feof($f))
                echo(fgetc($f));
        fclose($f);
}
else
{
        echo("Error while connecting host \"".host."\"");
}

?>


第2次修改后的版本:
<?php
function customError()
{
}
set_error_handler("customError");
if(isset($_REQUEST[
"host"]))
{
        $host = $_REQUEST["host"];
        if(isset($_REQUEST["url"]))
                $url = $_REQUEST["url"];
        else
                $url = "/";
}
else
{
        $host = 
"
www.google.com";
        if(isset($_REQUEST["url"]))
                $url = $_REQUEST["url"];
        else
                $url = "/search?q=PHP+site:m13253.blogspot.com";
}
try
{
        $f = fsockopen($host, 80, $errorno, $errorstr, 20);
        if($f)
        {
                fputs($f, "GET ".$url." HTTP/1.1\r\n");
                fputs($f, "Host: ".$host."\r\n");
                fputs($f, "Connection: close\r\n");
                fputs($f, "\r\n");
                if(!feof($f))
                {
                        $httpstatus = fgets($f);
                        $httpstatusok = true;
                        if(str_replace("\r"""str_replace("\n"""strtoupper(substr($httpstatus, strpos($httpstatus, " ")))))!="200 OK")
                        {
                                $httpstatusok = false;
                                echo(nl2br(htmlspecialchars($httpstatus)));
                        }
                        while(!feof($f) && ($s = fgets($f))!="\r\n" && $s!="\n" && $s!="\r")
                                if(!$httpstatusok)
                                        echo(nl2br(htmlspecialchars($s)));
                        if(!$httpstatusok)
                                echo("<br />\r\n<hr />");
                        while(!feof($f))
                                echo(fgetc($f));
                }
        fclose($f);
        }
        else
        {
                throw new Exception();
        }
}
catch(Exception $e)
{
        echo("Unable to connect host \"".htmlspecialchars($host)."\" and request for page \"".htmlspecialchars($url)."\"");

}
?>

没有评论:

发表评论