2

セキュリティ上の理由で一部のサーバーで使用できないため、この関数を使用したくありません。file_get_contents()をcURLに置き換えるにはどうすればよいですか?

以下の行は私のサーバーで問題を引き起こしています:

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

すべてのサーバーで機能するように、この行をcurlを使用する別の行に置き換えるにはどうすればよいですか?

4

1 に答える 1

10

これがあなたが使えるクリーンな関数です

$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;
}
于 2012-08-12T05:54:36.007 に答える