【问题标题】:php curl abrupt behaviorphp curl突然的行为
【发布时间】:2012-12-31 06:23:14
【问题描述】:

我正在使用 php curl 从 youtube 获取数据。有两个卷发。首先 curl 成功执行并从 youtube 检索访问令牌。第二卷曲使用此 oauth 令牌并检索用户信息。但它显示出意想不到的行为。有时它工作得非常好,但通常它会显示错误消息SSL Connection Timeout

如果代码有问题,那么为什么它有时会起作用 代码是

$url_userInfo =  'https://gdata.youtube.com/feeds/api/users/default?access_token='.$_SESSION['yt_access_token'].'&v=2&alt=json';    
$curl = curl_init();
$header[0] = "Accept: text/xml,application/xml,application/json, text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: ";


curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_URL, $url_userInfo);

curl_setopt($curl, CURLOPT_TIMEOUT, 20);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //needed for SSL

$content = curl_exec($curl);
echo curl_error($curl);
curl_close($curl);
$res = json_decode($content);

print_r($res);

【问题讨论】:

  • YouTube 没有这类东西的 API 吗? developers.google.com/youtube
  • 是的,我正在使用 youtube API
  • 你为什么不直接使用file_get_contents() 然后json_decode() 结果呢?
  • 应该,这是服务器端,不是客户端。
  • @RanaMuhammadUsman file_get_contents 可能会做同样的事情,但与 cURL 相比它非常有限。您也许可以使用上下文设置一些选项,但实际上没有任何理由在 cURL 上使用它。您是否尝试过增加超时?启用 cURL 详细选项并查找更详细的错误。

标签: php ssl curl


【解决方案1】:

试试这个:

$url_userInfo =  'https://gdata.youtube.com/feeds/api/users/default?access_token='.$_SESSION['yt_access_token'].'&v=2&alt=json';
$content = file_get_contents($url_userInfo);
$res = json_decode($content);
print_r($res);

【讨论】:

  • @Michael 我正在查看 API 文档,请求是通过 GET 使用令牌来检索 JSON 来处理的,那么这怎么会不起作用?
  • @cryptic 可以正常工作,但还有另一个错误: Cannot modify header information - headers already sent by (output started at...) :(
  • @RanaMuhammadUsman,你能发布整个错误信息吗?这是因为您在使用 session_start() 或 header() 之前在某处输出内容而引起的。我可以帮助查明问题,但需要完整的错误消息。您可以删除文件路径,但请保留文件名。
  • @cryptic Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\uno\controller\y.php:16) in D:\xampp\htdocs\uno\controller\y.php on line 52 第52行是header("Location: ../closewindow.php?status=added");
  • @RanaMuhammadUsman y.php 的第 16 行是什么,第 52 行是什么?
【解决方案2】:

您的连接可能需要很长时间才能从您的管中传输数据。所以要么 你可以增加 curl_setopt($curl, CURLOPT_TIMEOUT, 20);根据您的需要,或者您可以重新卷曲 它失败了。

【讨论】:

    【解决方案3】:

    您的标题中缺少一些信息。将访问令牌指定为 Authorization: Bearer HTTP 请求标头的值。同时删除 CURLOPT_CONNECTTIMEOUTCURLOPT_TIMEOUT 选项。 在this 之后,卷曲应该是:

    $ch = curl_init("http://gdata.youtube.com/feeds/api/videos/".$vid."?alt=json&v=2&access_token=".$token);
    
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth '.$token));
    curl_setopt($ch, CURLOPT_HTTPHEADER,  array('Content-length: 0'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, false);
    
    $output = curl_exec($ch);
    
    $http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    curl_close($ch);
    
    $result = json_decode($output, true);
    echo "<pre>"; print_r($result); echo "</pre>";
    

    【讨论】:

      猜你喜欢
      • 2015-02-27
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多