如何使用 cURL 删除 YouTube 视频 [英] How to delete a YouTube video using cURL

查看:21
本文介绍了如何使用 cURL 删除 YouTube 视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 YouTube API 发出 DELETE 请求,但是当我回显 curl_getinfo($ch, CURLINFO_HTTP_CODE) 时,它只是说 0,这意味着它失败了.

I am trying to make a DELETE request to the YouTube API but when I echo out curl_getinfo($ch, CURLINFO_HTTP_CODE), it just says 0 which mean it failed.

我已经检查了我的 API 密钥,这是正确的,访问代码也是如此,因为我正在使用刷新令牌获取访问令牌.

I have checked my API key and that's correct and so is the access code as i'm getting an access token using the refresh token.

cURL 命令是否正确或我应该查看访问代码?

Is the cURL commands correct or should i be looking at the access code?

$url = "https://www.googleapis.com/youtube/v3/videos?id=".$vID."MY_API_KEY";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/json','Authorization : Bearer '.$access_token));
    $result = curl_exec ($ch);
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close ($ch);

推荐答案

我这样做:

            $token = substr($key, 17, 83);

            $url = "https://www.googleapis.com/youtube/v3/videos?id=".$video_id;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/json','Authorization : Bearer '.$token));

            curl_exec ($ch);

            curl_close ($ch);

$key 包含我所有的 refresh_access 令牌,所以我使用 substr 函数来获取 access_token.

$key contains all my refresh_access token so I use substr function to get just the access_token.

在 var $url 中你可以看到比我退出的 ."MY_API_KEY" 部分因为不是必需的.它仅适用于 access_token

In the var $url you can see than I quit ."MY_API_KEY" part because is not necessary. It works fine only with the access_token

希望能帮到你.

这篇关于如何使用 cURL 删除 YouTube 视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆