用cURL替换file_get_contents()吗? [英] Replace file_get_contents() with cURL?
本文介绍了用cURL替换file_get_contents()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不想使用此功能,因为出于安全原因在某些服务器上不可用,如何用cURL替换file_get_contents()?
I don't want to use this function because to it's unavailability on some servers for security reasons, how can i replace file_get_contents() with cURL ?
下面的行导致我的服务器出现问题:
The line below is causing me a problem on my server :
$response = file_get_contents('http://images.google.com/images?hl=en&q=' . urlencode ($query) . '&imgsz=' . $size . '&imgtype=' . $type . '&start=' . (($page - 1) * 21));
我如何用另一行使用curl替换该行,使其在每台服务器上都能工作?
How can i replace the line with another one that uses curl so it will work on every server ?
推荐答案
这是可以使用的干净函数
here is a clean function you can use
$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;
}
这篇关于用cURL替换file_get_contents()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文