2016-05-03 12:55:46 0 Comments PHP Boy.Lee

compare PHP file_get_contents and curl for grab online

In PHP develop, we usually need grab data online, we have two tools: file_get_contents and curl, this topic will compare this two tools based on normal develop and give some advices

 

{Basic Usage}

> file_get_contents

$content = file_get_contents("http://yiilib.com"); //Html content

> curl

// 1. init
$ch = curl_init();

// 2. config
curl_setopt($ch, CURLOPT_URL, "http://yiilib.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

// 3. load html
$output = curl_exec($ch);

// 4. close curl
curl_close($ch);

 

{Compare}

> Used Time

Average Used Time for grab yiilib.com 20 times

Method Average Used Time
file_get_contents  2.1 - 2.6s
curl  0.5 - 0.8s

 

> stability

curl is better than file_get_contents

 

{Suggestion}

try use curl in your project

 

{Demo Code}

public static function load($url, $header=null, $https = false){
    if (empty($url)) {
    	return '';
    }//do nothing for empty
    
    //init
    $ch = curl_init();
    
    //set options
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //return result
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); //follow 301

    //user agent
    $userAgent = 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)';
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);   //set User-Agent
    
    
    //outtime
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 6); //connection timeout
    curl_setopt ($ch, CURLOPT_TIMEOUT, 20); //timeout
    
    //set more header
    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER , $header );
    }
    
    curl_setopt($ch, CURLOPT_HEADER, 0); //no need response header, faster
    
    
    //for https, skip verify paper and host
    if ($https) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    }
    
    //get result
    $output = curl_exec($ch);
    
    //close
    curl_close($ch);
    
    //return 
    return $output;
}