用cURL替换file_get_content()?

Raf*_*ari 2 php curl file-get-contents

我不想使用此功能,因为出于安全原因它在某些服务器上不可用,我如何用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));
Run Code Online (Sandbox Code Playgroud)

如何用另一个使用curl的线替换该线,以便它可以在每个服务器上运行?

the*_*mhz 8

这是一个你可以使用的清洁功能

$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;
}
Run Code Online (Sandbox Code Playgroud)