欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP之httpRequest

程序员文章站 2022-03-25 21:41:15

<?php 

    /**

    * respose a http request

    *

    * @param string $url

    * @param array $post

    * @param string $method

    * @param bool $returnheader

    * @param string $cookie

    * @param bool $bysocket

    * @param string $ip

    * @param integer $timeout

    * @param bool $block

    * @return string response

    */ 

    function httprequest($url,$post='',$method='get',$limit=0,$returnheader=false,$cookie='',$bysocket=false,$ip='',$timeout=15,$block=true) { 

       $return = ''

       $matches = parse_url($url); 

       !isset($matches['host']) && $matches['host'] = ''

       !isset($matches['path']) && $matches['path'] = ''

       !isset($matches['query']) && $matches['query'] = ''

       !isset($matches['port']) && $matches['port'] = ''

       $host = $matches['host']; 

       $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/'

       $port = !empty($matches['port']) ? $matches['port'] : 80; 

       if(strtolower($method) == 'post') { 

           $post = (is_array($post) and !empty($post)) ? http_build_query($post) : $post

           $out = "post $path http/1.0\r\n"

           $out .= "accept: */*\r\n"

           //$out .= "referer: $boardurl\r\n"; 

           $out .= "accept-language: zh-cn\r\n"

           $out .= "content-type: application/x-www-form-urlencoded\r\n"

           $out .= "user-agent: $_server[http_user_agent]\r\n"

           $out .= "host: $host\r\n"

           $out .= 'content-length: '.strlen($post)."\r\n"

           $out .= "connection: close\r\n"

           $out .= "cache-control: no-cache\r\n"

           $out .= "cookie: $cookie\r\n\r\n"

           $out .= $post

       } else

           $out = "get $path http/1.0\r\n"

           $out .= "accept: */*\r\n"

           //$out .= "referer: $boardurl\r\n"; 

           $out .= "accept-language: zh-cn\r\n"

           $out .= "user-agent: $_server[http_user_agent]\r\n"

           $out .= "host: $host\r\n"

           $out .= "connection: close\r\n"

           $out .= "cookie: $cookie\r\n\r\n"

       

       $fp = fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 

       if(!$fp) return ''; else

           $header = $content = ''

           stream_set_blocking($fp, $block); 

           stream_set_timeout($fp, $timeout); 

           fwrite($fp, $out); 

           $status = stream_get_meta_data($fp); 

           if(!$status['timed_out']) {//未超时 

               while (!feof($fp)) { 

                   $header .= $h = fgets($fp); 

                   if($h && ($h == "\r\n" ||  $h == "\n")) break

               

   

               $stop = false; 

               while(!feof($fp) && !$stop) { 

                   $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit)); 

                   $content .= $data

                   if($limit) { 

                       $limit -= strlen($data); 

                       $stop = $limit <= 0; 

                   

               

           

        fclose($fp); 

           return $returnheader ? array($header,$content) : $content

       

    

?>