php卷曲突然的行为

时间:2012-12-31 06:23:15

标签: php ssl curl

我使用php curl从youtube获取数据。有两个卷发。第一个curl成功执行并从youtube检索访问令牌。第二个curl使用此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);

3 个答案:

答案 0 :(得分:2)

试试这个:

$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);

答案 1 :(得分:1)

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

答案 2 :(得分:1)

您在标题中遗漏了一些信息。将访问令牌指定为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>";