首頁 > php教程 > PHP源码 > 主體

PHP 以 Post 方式请求网页数据

PHP中文网
發布: 2016-05-25 17:09:04
原創
1452 人瀏覽過

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;
     }    
  }
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!