且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用服务器为客户端隐藏 *** API

更新时间:2022-03-23 23:03:43

我不确定您想要做什么,但对于我参与的一个项目,我需要从 *** 获取特定的播放列表并将内容公开给访问者网站.

I am not sure what you want to do but for a project I worked on I needed to get a specific playlist from *** and make the contents public to the visitors of the website.

我所做的是一种代理.我设置了一个包含 api 密钥的 php 文件,然后让最终用户通过这个 php 文件获取 YT 内容.

What I did is a sort of proxy. I set up a php file contains the api key, and then have the end user get the YT content through this php file.

php 文件使用 curl 从 YT 中获取内容.

The php file gets the content form YT using curl.

希望能帮到你.

编辑 1

隐藏密钥的方法是将其放在服务器上的PHP文件中.这个 PHP 文件将连接到 *** 并在您的客户端页面上检索您想要的数据.

The way to hide the key is to put it in a PHP file on the server. This PHP file will the one connecting to *** and retrieving the data you want on your client page.

此代码示例,使用正确的 api 密钥和正确的播放列表 ID 将获得一个包含播放列表前 10 首曲目的 json 文件.

This example of code, with the correct api key and correct playlist id will get a json file with the 10 first tracks of the play list.

$resp 将包含 json 数据.要提取它,必须将其解码为例如关联数组.一旦进入数组,它就可以很容易地混合到将在客户端浏览器上呈现的 html 中.

The $resp will have the json data. To extract it, it has to be decoded for example into an associative array. Once in the array it can be easily mixed in to the html that will be rendered on the client browser.

<?php        
        $apiKey = "AIza...";
        $results = "10";
        $playList = "PL0WeB6UKDIHRyXXXXXXXXXX...";


        $request = "https://www.googleapis.com/***/v3/playlistItems?part=id,contentDetails,snippet&maxResults=" . $results . 
                   "&fields=items(contentDetails%2FvideoId%2Cid%2Csnippet(position%2CpublishedAt%2Cthumbnails%2Fdefault%2Ctitle))" .
                   "&playlistId=" . $playList . 
                   "&key=" . $apiKey;



        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => $request,
            CURLOPT_SSL_VERIFYPEER => false
        ));    

        $resp = curl_exec($curl);


        if (curl_errno($curl)) {
            $status = "CURL_ERROR";
        }else{
            // check the HTTP status code of the request
            $resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($resultStatus == 200) {
                $status = "OK";
                //Do something with the $resp which is in JSON format.
                //Like decoding it into an associative array
            } else {
                $status = "YT_ERROR";
            }
        }

        curl_close($curl);
?>
<html>
<!-- your html here -->
</html>

注意:CURLOPT_SSL_VERIFYPEER 设置为 false.这是在开发中.对于产品,它应该是真的.

Note: CURLOPT_SSL_VERIFYPEER is set to false. This is in development. For prod it should be true.

另请注意,以这种方式使用 api,您可以限制对 api 密钥的调用,将它们绑定到您的域.您可以在 googla api 控制台中执行此操作.(制作提示)

Also note that using the api this way, you can restrict the calls to your api key bounding them to your domain. You do that in the googla api console. (Tip for production)