fsockopen是PHP提供的一个网络函数,用于打开打开一个网络连接或者一个Unix套接字连接。这是一种比较底层的写法,甚至fsockopen 返回的是没有处理过的数据,包括数据的长度数据内容和数据的结束符。而cURL系列函数就好用的多。CURL还会自动对DNS信息进行缓存,效率更高。偏底层的东西肯定更强大,学习成本更高。

使用fsockopen可以编写socket程序, fsockopen执行后返回的是文件指针,所以文件相关函数fgets()、fgetss()、fputs()、fclose() 与 feof(),尽情用吧,相信初学者,都会被它拼接http header恶心了吧。

下面向大家介绍fsockopen的基本语法:

resource fsockopen(string $hostname [,int $port = -1 [, int &$errno [,string &$errstr [, float $timeout = ini_get(\"default_socket_timeout\") ]]]] )

启动一个套接字连接到指定的主机的资源,php支持在互联网领域的目标和unix在所支持的套接字传输列表说明,所支持的传输列表也可以检索使用stream_get_transports().

该插座预设会被启用,阻塞模式,你可以切换到非阻塞模式使用stream_set_blocking()。

 

1、fsockopen模拟GET:

$fp = fsockopen(\"www.manongjc.com\", 80, $errno, $errstr, 30); 
 if (!$fp) { 
  echo \"$errstr ($errno) \"; 
 } else { 
  $out = \"get / http/1.1 \"; 
  $out .= \"host: www.manongjc.com\"; 
  $out .= \"connection: close \"; 
  fwrite($fp, $out); 
  while (!feof($fp)) { 
   echo fgets($fp, 128); 
  } 
  fclose($fp); 
 }

 

2、fsockopen模拟post:

$post =1; 
$url = parse_url($url); 
$host =\'http://www.manongjc.com\'; 
$path =\'/\'; 
$query =\'?action=phpfensi.com\'; 
$port =80; 
  
if($post) { 
  $out = \"post $path http/1.0 \"; 
  $out .= \"accept: */* \"; 
  //$out .= \"referer: $boardurl \"; 
  $out .= \"accept-language: zh-cn \"; 
  $out .= \"content-type: application/x-www-form-urlencoded \"; 
  $out .= \"user-agent: $_server[http_user_agent] \"; 
  $out .= \"host: $host \"; 
  $out .= \'content-length: \'.strlen($post).\" \"; 
  $out .= \"connection: close \"; 
  $out .= \"cache-control: no-cache \"; 
  $out .= \"cookie: $cookie \"; 
  $out .= $post; 
 } else { 
  $out = \"get $path http/1.0 \"; 
  $out .= \"accept: */* \"; 
  //$out .= \"referer: $boardurl \"; 
  $out .= \"accept-language: zh-cn \"; 
  $out .= \"user-agent: $_server[http_user_agent] \"; 
  $out .= \"host: $host \"; 
  $out .= \"connection: close \"; 
  $out .= \"cookie: $cookie \"; 
 } 
 $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout); 
 if(!$fp)  
 { 
  return \'\';//note $errstr : $errno  
 } else { 
  return \'成功访问\'; 
 }

 

3、封装好的fsockopen函数实现POST、GET请求

function http_request($url, $method = \'GET\', $postfields = NULL, $headers = array()) {
 
    $parse = parse_url($url);
 
    isset($parse[\'host\']) ||$parse[\'host\'] = \'\';
    isset($parse[\'path\']) || $parse[\'path\'] = \'\';
    isset($parse[\'query\']) || $parse[\'query\'] = \'\';
    isset($parse[\'port\']) || $parse[\'port\'] = \'\';
 
    $path = $parse[\'path\'] ? $parse[\'path\'].($parse[\'query\'] ? \'?\'.$parse[\'query\'] : \'\') : \'/\';
    $host = $parse[\'host\'];
 
    //协议
    if ($parse[\'scheme\'] == \'https\') {
        $version = \'1.1\';
        $port = empty($parse[\'port\']) ? 443 : $parse[\'port\'];
        $host = \'ssl://\'.$host;
    } else {
        $version = \'1.0\';
        $port = empty($parse[\'port\']) ? 80 : $parse[\'port\'];
    }
 
    //Headers
    $headers[] = \"Host: {$parse[\'host\']}\";
    $headers[] = \'Connection: Close\';
    $headers[] = \"User-Agent: $_SERVER[HTTP_USER_AGENT]\";  
    $headers[] = \'Accept: */*\';
 
    //包体信息
    if ($method == \'POST\') {
        if(is_array($postfields)){
            $postfields = http_build_query($postfields);
        }
        $headers[] = \"Content-type: application/x-www-form-urlencoded\";
        $headers[] = \'Content-Length: \'.strlen($postfields);
        $out = \"POST $path HTTP/$version\\r\\n\".join(\"\\r\\n\", $headers).\"\\r\\n\\r\\n\".$postfields;
    } else {
        $out = \"GET $path HTTP/$version\\r\\n\".join(\"\\r\\n\", $headers).\"\\r\\n\\r\\n\";
    }
 
    //发送请求
    $limit = 0;
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
 
    if (!$fp) {
        exit(\'Failed to establish socket connection: \'.$url);
    } else {
        $header = $content = \'\';
        //集阻塞/非阻塞模式流,$block==true则应用流模式
        stream_set_blocking($fp, true);
         //设置流的超时时间
        stream_set_timeout($fp, 30);
        fwrite($fp, $out);
        //从封装协议文件指针中取得报头/元数据
        $status = stream_get_ _data($fp);
 
        if (!$status[\'timed_out\']) { //未超时
            while (!feof($fp)) {
                $header .= $h = fgets($fp);
                if ($h && ($h == \"\\r\\n\" ||  $h == \"\\n\")) break;
                if (strpos($h, \'Content-Length:\') !== false) {
                    $limit = intval(substr($header, 15));
                }
            }
 
            $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);
 
        //unchunk
        $content = preg_replace_callback(
            \'/(?:(?:\\r\\n|\\n)|^)([0-9A-F]+)(?:\\r\\n|\\n){1,2}(.*?)\'.
            \'((?:\\r\\n|\\n)(?:[0-9A-F]+(?:\\r\\n|\\n))|$)/si\',
            create_function(
                \'$matches\',
                \'return hexdec($matches[1]) == strlen($matches[2]) ? $matches[2] : $matches[0];\'
            ),
            $content
        );
 
        return $content;
    }
}
收藏 打印