gpt4 book ai didi

php curl 突然的行为

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:15 25 4
gpt4 key购买 nike

我正在使用 php curl 从 youtube 获取数据。有两个卷发。首先 curl 成功执行并从 youtube 检索访问 token 。第二个 curl 使用此 oauth token 并检索用户信息。但它显示出意想不到的行为。有时它工作得非常好,但通常它会显示错误消息 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);

最佳答案

试试这个:

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

关于php curl 突然的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14098241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com