我试图通过YoutubeAPi使用curl删除其ID的视频。
在下面的代码中,curl_errno()返回CURLE_URL_MALFORMAT错误。
$link = urlencode("https://www.googleapis.com/youtube/v3/videos?id=my_Video_ID&key=my_API_KEY");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$test = curl_errno($ch);
echo $test . "n";
$result = json_decode($result);
curl_close($ch);
return $result;
代码有什么问题
不需要对URL (urlencode
)进行编码。只需将URL字符串分配给您的$link
变量。(阅读文档)
但是,最好对参数值进行编码:
test.php
$id = urlencode('AAAAABBBCCC');
$key = urlencode('AAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCCCCCCC');
$link = "https://www.googleapis.com/youtube/v3/videos?id=$id&key=$key";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
测试php test.php
输出string(238) "{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
"
API实际工作。现在需要设置正确的id
和key
参数和授权令牌,详见官方文档:
您必须为每次插入、更新和操作发送授权令牌删除请求。