<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    yxhxj2006

    常用鏈接

    統(tǒng)計(jì)

    最新評(píng)論

    PHP利用Curl、socket、file_get_contents POST數(shù)據(jù)

    <?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讀取網(wǎng)絡(luò)文件 curl, fsockopen ,file_get_contents 幾個(gè)方法的效率對(duì)比

    curl效率及穩(wěn)定原來(lái)可以遠(yuǎn)遠(yuǎn)超越file_get_contents

     

    最近需要獲取別人網(wǎng)站上的音樂(lè)數(shù)據(jù)。用了file_get_contents函數(shù),但是總是會(huì)遇到獲取失敗的問(wèn)題,盡管按照手冊(cè)中的 例子設(shè)置了超時(shí),可多數(shù)時(shí)候不會(huì)奏效:


    $config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
       ’timeout’ => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不奏效
       )
      ));

    這時(shí)候,看一下服務(wù)器的連接池,會(huì)發(fā)現(xiàn)一堆類似的錯(cuò)誤,讓我頭疼萬(wàn)分:
    file_get_contents(http://***): failed to open stream…

    現(xiàn)在改用了curl庫(kù),寫了一個(gè)函數(shù)替換:
    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;
    }

    如此,除了真正的網(wǎng)絡(luò)問(wèn)題外,沒(méi)再出現(xiàn)任何問(wèn)題。
    這是別人做過(guò)的關(guān)于curl和file_get_contents的測(cè)試:
    file_get_contents抓取google.com需用秒數(shù):

     

    2.31319094

    2.30374217
    2.21512604
    3.30553889
    2.30124092

    curl使用的時(shí)間:

     

    0.68719101

    0.64675593
    0.64326
    0.81983113
    0.63956594

    差距很大?呵呵,從我使用的經(jīng)驗(yàn)來(lái)說(shuō),這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大。

    建議對(duì)網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦!

     

     

     

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

    php fsockopen

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

    方法2: 用fopen打開(kāi)url, 以get方式獲取內(nèi)容
    <?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函數(shù),以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函數(shù)打開(kāi)url,以get方式獲取完整的數(shù)據(jù),包括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函數(shù)打開(kāi)url,以POST方式獲取完整的數(shù)據(jù),包括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庫(kù),使用curl庫(kù)之前,可能需要查看一下php.ini是否已經(jīng)打開(kāi)了curl擴(kuò)展

    <?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 三個(gè)函數(shù) 都可以實(shí)現(xiàn)采集模擬發(fā)言 。 三者有什么區(qū)別,或者講究么  

    趙永斌:
    有些時(shí)候用file_get_contents()調(diào)用外部文件,容易超時(shí)報(bào)錯(cuò)。換成curl后就可以.具體原因不清楚
    curl 效率比f(wàn)ile_get_contents()和fsockopen()高一些,原因是CURL會(huì)自動(dòng)對(duì)DNS信息進(jìn)行緩存(亮點(diǎn)啊 有我待親測(cè))

    范佳鵬:
    file_get_contents curl fsockopen
    在當(dāng)前所請(qǐng)求環(huán)境下選擇性操作,沒(méi)有一概而論:
    具我們公司開(kāi)發(fā)KBI應(yīng)用來(lái)看:
    剛開(kāi)始采用:file_get_contents
    后來(lái)采用:fsockopen
    最后到至今采用:curl

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

    潘少寧-騰訊:

    file_get_contents 雖然可以獲得某URL的內(nèi)容,但不能post  get啊。
    curl 則可以post和get啊。還可以獲得head信息
    而socket則更底層。可以設(shè)置基于UDP或是TCP協(xié)議去交互
    file_get_contents 和 curl 能干的,socket都能干。
    socket能干的,curl 就不一定能干了
    file_get_contents  更多的時(shí)候 只是去拉取數(shù)據(jù)。效率比較高  也比較簡(jiǎn)單。
    趙的情況這個(gè)我也遇到過(guò),我通過(guò)CURL設(shè)置host 就OK了。  這和網(wǎng)絡(luò)環(huán)境有關(guān)系

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


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 人妻丰满熟妇无码区免费| 美女又黄又免费的视频| 亚洲国产日韩在线成人蜜芽| 亚洲精品私拍国产福利在线| 久久狠狠躁免费观看| 亚洲欧美国产国产一区二区三区| 国产成人亚洲合集青青草原精品| 亚洲精品高清无码视频| 九月丁香婷婷亚洲综合色| 国产视频精品免费| 在线视频免费国产成人| 国产免费一区二区三区VR| 全部免费毛片免费播放| 亚洲精品视频免费观看| 亚洲日本中文字幕天堂网| 亚洲人成在线播放网站| 久久久久亚洲av无码专区导航 | 亚洲免费在线观看| 一个人晚上在线观看的免费视频| 亚洲欧洲另类春色校园网站| 亚洲砖码砖专无区2023| 九九精品国产亚洲AV日韩| 亚洲成a人片7777| 亚洲日韩一区精品射精| 免费观看亚洲人成网站| 中文字幕在线视频免费观看| 最新亚洲人成网站在线观看| 无码免费又爽又高潮喷水的视频| 国产精品视频全国免费观看| 午夜免费福利视频| 成年私人影院免费视频网站| 亚洲AV无码一区二三区| 亚洲高清国产拍精品26U| 亚洲av无码久久忘忧草| 精品在线免费视频| 免费国产在线视频| 久久精品私人影院免费看| 4虎永免费最新永久免费地址| 亚洲免费精彩视频在线观看| 中文字幕无码成人免费视频| 亚洲国产精品国产自在在线 |