我尝试使用curl通过YoutubeAPi通过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;
代码有什么问题?
答案 0 :(得分:1)
您无需对网址进行编码(urlencode
)。只需将URL字符串分配给$link
变量即可。 (阅读docs。)
但是,最好对参数值进行编码:
<强> 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
参数和授权令牌,如official docs中所述:
您必须为每次插入,更新和发送授权令牌 删除请求。