我最近开始学习curl通过PHP访问YouTube API,但由于某种原因,卷曲响应是空的。首先,响应为我提供301 error
,因此我使用FOLLOW LOCATION
来关注重定向,但现在我的请求甚至没有返回标题。
这是我的CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://youtube.com/oembed');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
array('url' => 'https://www.youtube.com/watch?v=oorK4RPgZ8Q', 'format' => 'json')
));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
curl_close($ch);
此代码之前运行良好,仍适用于SoundCloud API。他们是否有可能阻止我的请求?
编辑:这是我删除FOLLOW LOCATION
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://youtube.com/oembed
Date: Fri, 02 Feb 2018 10:17:41 GMT
Content-Type: text/html
Server: YouTube Frontend Proxy
X-XSS-Protection: 1; mode=block
答案 0 :(得分:2)
更改了网址,并使用'curl_close'切换'curl_exec':
$ch = curl_init();
$url = 'http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=oorK4RPgZ8Q&format=json';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
/*
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(
array('url' => 'https://www.youtube.com/watch?v=oorK4RPgZ8Q', 'format' => 'json')
));
*/
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$output = curl_exec($ch);
curl_close($ch);
修改强>:
curl_setopt($ch, CURLOPT_URL, 'http://youtube.com/oembed');
应该是
curl_setopt($ch, CURLOPT_URL, 'http://www.youtube.com/oembed');
(注意添加的'www')