用cURL替换file_get_contents()?

问题描述 投票:2回答:1

我不想使用此功能,因为出于安全原因它在某些服务器上不可用,我如何用cURL替换file_get_contents()?

以下行导致我的服务器出现问题:

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

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

php curl file-get-contents
1个回答
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;
}
© www.soinside.com 2019 - 2024. All rights reserved.