Home > php教程 > PHP源码 > body text

PHP 以 Post 方式请求网页数据

PHP中文网
Release: 2016-05-25 17:09:04
Original
1504 people have browsed it

PHP 以 Post 方式请求网页数据

/**
   * Post 方式请求网页数据
   *
   * @param string $url     网页地址
   * @prarm string $host    主机
   * @param string $session 会话值
   * @prarm string $type    类型(POST、GET)
   * @prarm string $port    端口
   * @prarm string $data    数据
   */
  function getPageConent( $url, $host,  $data = "" ,$session = "", $type = "POST", $port = "") {
      
      if( empty($port) ) $port = 80;     
      
      /* 请求数据 */
      $post_data = $data;
     $lenght = strlen($post_data);
     
     $headers  = "{$type} {$url} HTTP/1.1\r\n";
     $headers .= "Accept: * /*\r\n";
     $headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
     $headers .= "User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6; CIBA; .NET CLR 4.0.20506)\r\n";
     if($session != "" ) $headers .= "Cookie:JSESSIONID={$session}\r\n";
     $headers .= "Host: {$host}:{$port}\r\n";
     $headers .= "Content-Length: {$lenght}\r\n";
     $headers .= "Connection: Close\r\n\r\n";
     $headers .= $post_data;
 
     if( $fp = fsockopen( $host, $port, $errno, $errstr, 100) ) {
         fwrite($fp, $headers);        
           $header   = fread($fp, 1024);    
           $content  = fread($fp, 1024);    
           $content .= fread($fp, 1024);
           $content .= fread($fp, 1024);
           $content .= fread($fp, 1024);
         fclose($fp);        
     }    
     if( $data != "" ) {
         echo $headers;
         echo "";
         echo $header;
         echo "";
         echo $content;
         echo "";
         exit;
     } else {
         return $content;
     }    
  }
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template