【问题标题】:Essential curl headers to be included? [closed]要包含的基本卷曲标题? [关闭]
【发布时间】:2013-11-27 02:03:54
【问题描述】:

我正在制作一个使用 Youtube Data API 的应用程序。我认为 CURL 总是比使用 PHP 的 file_get_contents() 更好的选择。使用 CURL,我需要知道哪些所有标头都应包含在请求中以优化性能。

我需要设置一些超时设置、错误处理方法等;

提前感谢。

【问题讨论】:

标签: php curl youtube youtube-api youtube-data-api


【解决方案1】:

您可以为 file_get_contents (http://us1.php.net/manual/en/function.stream-context-create.php) 使用 http 包装器:

$options = array(
    'http'=>array(
        'method'=>"GET",
        'timeout' => 60
    )
);

$context = stream_context_create($options);
file_get_contents("http://google.com", false, $context);
print_r($http_response_header);

【讨论】:

  • 那么你建议我使用 file_get_contents 而不是 curl 吗?
  • 这取决于你。无论如何都会使用 CURL 扩展。
【解决方案2】:
    $ch = curl_init();


    curl_setopt($ch, CURLOPT_URL,            $url);
    //curl_setopt($ch, CURLOPT_URL, $IP);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array('User-Agent: 1.11.4 Red Hat modified',"Host : $host",'Connection: Keep-Alive'));//it might need
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER,         true);
    curl_setopt($ch, CURLOPT_NOBODY,         true);//just get the header info only
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,       $timeout);//set your time out
    $data = curl_exec($ch);
    return  $data;

【讨论】:

    【解决方案3】:

    你可以试试这样的:

        $ch = curl_init('your url');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, 20);
                curl_setopt($ch,CURLOPT_HTTPHEADER,array(
                                'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1',
                                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                                'Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3',
                                'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7',
                                'Connection: keep-alive',
                                'Pragma: no-cache',
                                'Cache-Control: no-cache'
                ));
    
                $result = curl_exec($ch);
    if($result == FALSE) { 
    var_dump(curl_error($ch)); //handling error
    } else { //success
    echo $result;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多