yxhxj2006

          常用鏈接

          統計

          最新評論

          PHP利用Curl、socket、file_get_contents POST數據

          <?php 
          /** 
          * Socket版本 
          * 使用方法: 
          * $post_string = "app=socket&version=beta"; 
          * request_by_socket('facebook.cn','/restServer.php',$post_string); 
          */ 
          function request_by_socket($remote_server,$remote_path,$post_string,$port 80,$timeout 30
          ){ 
              
          $socket fsockopen($remote_server,$port,$errno,$errstr,$timeout
          ); 
               if (!
          $socket) die("$errstr($errno)"
          ); 
              
              
          fwrite($socket,"POST $remote_path HTTP/1.0\r\n"
          ); 
              
          fwrite($socket,"User-Agent: Socket Example\r\n"
          ); 
              
          fwrite($socket,"HOST: $remote_server\r\n"
          ); 
              
          fwrite($socket,"Content-type: application/x-www-form-urlencoded\r\n"
          ); 
              
          fwrite($socket,"Content-length: ".strlen($post_string)+8."\r\n"
          ); 
              
          fwrite($socket,"Accept:*/*\r\n"
          ); 
              
          fwrite($socket,"\r\n"
          ); 
              
          fwrite($socket,"mypost=$post_string\r\n"
          ); 
              
          fwrite($socket,"\r\n"
          ); 
              
              
          $header ""

               while (
          $str trim(fgets($socket,4096
          ))) { 
                  
          $header.=$str

               } 
              
              
          $data ""

               while (!
          feof($socket
          )) { 
                  
          $data .= fgets($socket,4096
          ); 
               } 
              
               return 
          $data

          }

          /** 
          * Curl版本 
          * 使用方法: 
          * $post_string = "app=request&version=beta"; 
          * request_by_curl('http://facebook.cn/restServer.php',$post_string); 
          */ 
          function request_by_curl($remote_server,$post_string
          ){ 
              
          $ch curl_init
          (); 
              
          curl_setopt($ch,CURLOPT_URL,$remote_server
          ); 
              
          curl_setopt($ch,CURLOPT_POSTFIELDS,'mypost='.$post_string
          ); 
              
          curl_setopt($ch,CURLOPT_RETURNTRANSFER,true
          ); 
              
          curl_setopt($ch,CURLOPT_USERAGENT,"Jimmy's CURL Example beta"
          ); 
              
          $data curl_exec($ch
          ); 
              
          curl_close($ch
          ); 
               return 
          $data


          /** 
          * 其它版本 
          * 使用方法: 
          * $post_string = "app=request&version=beta"; 
          * request_by_other('http://facebook.cn/restServer.php',$post_string); 
          */ 
          function request_by_other($remote_server,$post_string
          ){ 
              
          $context 
          = array( 
                  
          'http'
          =>array( 
                      
          'method'=>'POST'

                      
          'header'=>'Content-type: application/x-www-form-urlencoded'."\r\n"

                                
          'User-Agent : Jimmy\'s POST Example beta'."\r\n"

                                
          'Content-length: '.strlen($post_string)+8

                      
          'content'=>'mypost='.$post_string

                   ); 
              
          $stream_context stream_context_create($context
          ); 
              
          $data file_get_contents($remote_server,FALSE,$stream_context
          ); 
               return 
          $data

          }

          function curl_file_get_contents($durl) {     

                  $ch = curl_init ();     
                  curl_setopt ( $ch, CURLOPT_URL, $durl );        
                  curl_setopt ( $ch, CURLOPT_POST, 1 );
                  curl_setopt ( $ch, CURLOPT_TIMEOUT, 5 );        
                  curl_setopt ( $ch, CURLOPT_USERAGENT, _USERAGENT_ );        
                  curl_setopt ( $ch, CURLOPT_REFERER, _REFERER_ );        
                  curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
                  $r = curl_exec ( $ch );
                  curl_close ( $ch );     
                  return $r;

              }




          php讀取網絡文件 curl, fsockopen ,file_get_contents 幾個方法的效率對比

          curl效率及穩定原來可以遠遠超越file_get_contents

           

          最近需要獲取別人網站上的音樂數據。用了file_get_contents函數,但是總是會遇到獲取失敗的問題,盡管按照手冊中的 例子設置了超時,可多數時候不會奏效:


          $config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
             ’timeout’ => 5//這個超時時間不穩定,經常不奏效
             )
            ));

          這時候,看一下服務器的連接池,會發現一堆類似的錯誤,讓我頭疼萬分:
          file_get_contents(http://***): failed to open stream…

          現在改用了curl庫,寫了一個函數替換:
          function curl_file_get_contents($durl){
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $durl);
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
            curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $r = curl_exec($ch);
            curl_close($ch);
             return $r;
          }

          如此,除了真正的網絡問題外,沒再出現任何問題。
          這是別人做過的關于curl和file_get_contents的測試:
          file_get_contents抓取google.com需用秒數:

           

          2.31319094

          2.30374217
          2.21512604
          3.30553889
          2.30124092

          curl使用的時間:

           

          0.68719101

          0.64675593
          0.64326
          0.81983113
          0.63956594

          差距很大?呵呵,從我使用的經驗來說,這兩個工具不只是速度有差異,穩定性也相差很大。

          建議對網絡數據抓取穩定性要求比較高的朋友使用上面的 curl_file_get_contents函數,不但穩定速度快,還能假冒瀏覽器欺騙目標地址哦!

           

           

           

          看到的其他文章收藏于此===============================

          php fsockopen

          方法1: 用file_get_contents 以get方式獲取內容
          <?php
          $url='http://www.domain.com/';
          $html = file_get_contents($url);
          echo $html;
          ?>

          方法2: 用fopen打開url, 以get方式獲取內容
          <?php
          $fp = fopen($url, 'r');
          stream_get_meta_data($fp);
          while(!feof($fp)) {
          $result .= fgets($fp, 1024);
          }
          echo "url body: $result";
          fclose($fp);
          ?>



          方法3:用file_get_contents函數,以post方式獲取url
          <?php
          $data = array ('foo' => 'bar');
          $data = http_build_query($data);

          $opts = array (
          'http' => array (
          'method' => 'POST',
          'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
          "Content-Length: " . strlen($data) . "\r\n",
          'content' => $data
          )
          );

          $context = stream_context_create($opts);
          $html = file_get_contents('http://localhost/e/admin/test.html', false, $context);

          echo $html;
          ?>


          方法4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body

          <?php
          function get_url ($url,$cookie=false)
          {
          $url = parse_url($url);
          $query = $url[path]."?".$url[query];
          echo "Query:".$query;
          $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
          if (!$fp) {
          return false;
          } else {
          $request = "GET $query HTTP/1.1\r\n";
          $request .= "Host: $url[host]\r\n";
          $request .= "Connection: Close\r\n";
          if($cookie) $request.="Cookie:   $cookie\n";
          $request.="\r\n";
          fwrite($fp,$request);
          while()) {
          $result .= @fgets($fp, 1024);
          }
          fclose($fp);
          return $result;
          }
          }
          //獲取url的html部分,去掉header
          function GetUrlHTML($url,$cookie=false)
          {
          $rowdata = get_url($url,$cookie);
          if($rowdata)
          {
          $body= stristr($rowdata,"\r\n\r\n");
          $body=substr($body,4,strlen($body));
          return $body;
          }

              return false;
          }
          ?>



          方法5:用fsockopen函數打開url,以POST方式獲取完整的數據,包括header和body

          <?php
          function HTTP_Post($URL,$data,$cookie, $referrer="")
          {

              // parsing the given URL
          $URL_Info=parse_url($URL);

              // Building referrer
          if($referrer=="") // if not given use this script as referrer
          $referrer="111";

              // making string from $data
          foreach($data as $key=>$value)
          $values[]="$key=".urlencode($value);
          $data_string=implode("&",$values);

              // Find out which port is needed - if not given use standard (=80)
          if(!isset($URL_Info["port"]))
          $URL_Info["port"]=80;

              // building POST-request:
          $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
          $request.="Host: ".$URL_Info["host"]."\n";
          $request.="Referer: $referer\n";
          $request.="Content-type: application/x-www-form-urlencoded\n";
          $request.="Content-length: ".strlen($data_string)."\n";
          $request.="Connection: close\n";

              $request.="Cookie:   $cookie\n";

              $request.="\n";
          $request.=$data_string."\n";

              $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
          fputs($fp, $request);
          while(!feof($fp)) {
          $result .= fgets($fp, 1024);
          }
          fclose($fp);

              return $result;
          }

          ?>


          方法6:使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展

          <?php
          $ch = curl_init();
          $timeout = 5;
          curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
          curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
          $file_contents = curl_exec($ch);
          curl_close($ch);

          echo $file_contents;
          ?>

           

           

           

           

          php中 curl, fsockopen ,file_get_contents 三個函數 都可以實現采集模擬發言 。 三者有什么區別,或者講究么  

          趙永斌:
          有些時候用file_get_contents()調用外部文件,容易超時報錯。換成curl后就可以.具體原因不清楚
          curl 效率比file_get_contents()和fsockopen()高一些,原因是CURL會自動對DNS信息進行緩存(亮點啊 有我待親測)

          范佳鵬:
          file_get_contents curl fsockopen
          在當前所請求環境下選擇性操作,沒有一概而論:
          具我們公司開發KBI應用來看:
          剛開始采用:file_get_contents
          后來采用:fsockopen
          最后到至今采用:curl

          (遠程)我個人理解到的表述如下(不對請指出,不到位請補充)
          file_get_contents 需要php.ini里開啟allow_url_fopen,請求http時,使用的是http_fopen_wrapper,不會keeplive.curl是可以的。
          file_get_contents()單個執行效率高,返回沒有頭的信息。
          這個是讀取一般文件的時候并沒有什么問題,但是在讀取遠程問題的時候就會出現問題。
          如果是要打一個持續連接,多次請求多個頁面。那么file_get_contents和fopen就會出問題。
          取得的內容也可能會不對。所以做一些類似采集工作的時候,肯定就有問題了。
          sock較底層,配置麻煩,不易操作。 返回完整信息。

          潘少寧-騰訊:

          file_get_contents 雖然可以獲得某URL的內容,但不能post  get啊。
          curl 則可以post和get啊。還可以獲得head信息
          而socket則更底層。可以設置基于UDP或是TCP協議去交互
          file_get_contents 和 curl 能干的,socket都能干。
          socket能干的,curl 就不一定能干了
          file_get_contents  更多的時候 只是去拉取數據。效率比較高  也比較簡單。
          趙的情況這個我也遇到過,我通過CURL設置host 就OK了。  這和網絡環境有關系

          posted on 2014-12-31 19:05 奮斗成就男人 閱讀(315) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 射阳县| 清涧县| 锡林浩特市| 榆中县| 宜城市| 绥滨县| 梁平县| 黔东| 鄄城县| 文山县| 凌海市| 镇赉县| 宁波市| 宜阳县| 剑阁县| 平安县| 温泉县| 会宁县| 石家庄市| 钟祥市| 宁安市| 大名县| 中卫市| 吐鲁番市| 顺义区| 高雄县| 莱芜市| 兰西县| 红河县| 寿光市| 盐山县| 崇义县| 叶城县| 大埔县| 忻州市| 太湖县| 宁乡县| 平山县| 巴彦县| 桂平市| 泾阳县|