2012-08-12 40 views
2

我不想使用这个函数,因为它出于安全原因在某些服务器上不可用,我怎样才能用cURL替换file_get_content()?用cURL替换file_get_content()?

下面的线造成了我一个问题,我的服务器上:

$response = file_get_contents('http://images.google.com/images?hl=en&q=' . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21)); 

我如何可以替换使用卷曲所以它会在每个服务器上运行另一行?

回答

7

这里是一个清洁的功能,您可以使用

$response = get_data('http://images.google.com/images?hl=en&q=' . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21)); 

function get_data($url) 
{ 
    $ch = curl_init(); 
    $timeout = 5; 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
    $data = curl_exec($ch); 
    curl_close($ch); 
    return $data; 
} 
+0

哪里变量应该用来代替$回应? – 2012-08-12 05:56:31

+0

返回将返回数据 – themis 2012-08-12 05:58:38

+0

作品像冠军!谢谢 – 2012-08-12 06:05:21