如何使用cURL删除YouTube视频(How to delete a YouTube video

2019-10-21 03:52发布

我试图做一个DELETE请求到YouTube的API,但是当我回声出curl_getinfo($ch, CURLINFO_HTTP_CODE)它只是说0这意味着失败。

我检查了我的API密钥,这是正确的,所以是因为我使用的刷新令牌获得访问令牌的访问代码。

是卷曲命令是否正确或者我应该寻找在接入代码?

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

Answer 1:

我这样做:

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

$键包含我所有的refresh_access令牌所以我用SUBSTR函数只获取的access_token。

在变量$网址,你可以看到比我不干了。“MY_API_KEY”部分原因是没有必要的。 它工作正常,只能用的access_token

希望我帮你。



文章来源: How to delete a YouTube video using cURL