柳暗花明笔记

    PHP的HTTP请求

    所属分类:[ php&web ] 猫哲学家 发表于:2012-07-31 22:45 浏览:435

    一.GET方式:

    $host_ip = '127.0.0.1'; $host_domain = 'www.example.com'; $getway = '/index.php?uid=xxx&pw=xxx'; GET_HTTP($host_ip,$host_domain,$getway); function GET_HTTP($host_ip,$host_domain,$getway) {     $fp = fsockopen ($host_ip, 80, $errno, $errstr, 30);     if (!$fp)     {         echo "$errstr ($errno) \n";     }     else     {         $out  = "GET ".$getway." HTTP/1.1\r\n";         $out .= "Host: ".$host_domain."\r\n";         $out .= "Accept: */*\r\n\r\n";     fwrite($fp, $out);     while (!feof($fp))     {         $content.=fgets($fp, 128);     }     fclose($fp);     $pager=explode("\n",$content);     $get_content = $pager[4];     }     return $get_content; }

     

    二.POST方式:

    $host = 'http://www.example.com/index.php'; $query = 'uid=xxx&pw=xxx'; POST_HTTP($host,$query); function POST_HTTP($host,$query,$others='') {     $path=explode('/',$host);     $host=$path[0];     unset($path[0]);     $path='/'.(implode('/',$path));     $post="POST $path HTTP/1.1\r\nHost: $host\r\n";     $post.="Content-type: application/x-www-form-";     $post.="urlencoded\r\n${others}";     $post.="User-Agent: Mozilla 4.0\r\nContent-length: ";     $post.=strlen($query)."\r\nConnection: close\r\n\r\n$query";     $h=fsockopen($host,80);     fwrite($h,$post);     for($a=0,$r='';!$a;)     {     $b=fread($h,8192);     $r.=$b;     $a=(($b=='')?1:0);     }     fclose($h);     return $r; }

    标签: php  http 
    上一编文章 drupal view 在火狐下可以看,IE google 下不能看的问题 下一编文章 [PHP]模拟http请求的类
    最近文章
    发表评论