【问题标题】:Youtube api Where will write this codeYoutube api 哪里会写这段代码
【发布时间】:2014-05-10 01:33:49
【问题描述】:

我的 php 知识太糟糕了。我正在使用 YouTube-api。将在哪里编写此代码:Retrieve Youtube Channel info for "Vanity" channel

【问题讨论】:

    标签: youtube-api


    【解决方案1】:

    如果你说的是这一行:

    GET https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}
    

    您只是发出一个获取请求,您可以使用file_get_contents 为您获取响应:

    $response = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=UC6ltI41W4P14NShIBHU8z1Q&key={YOUR_API_KEY}");
    

    两个音符:

    • 您必须将 {YOUR_API_KEY} 替换为开发人员密钥。您可以轻松地从 youtube 请求一个:http://code.google.com/apis/youtube/dashboard/

    • 这只是一行代码中的一个示例,我建议您使用更好的方法来发出此请求,如下所示:

      // Encode the parameters of the link
      function encode_param($params) {
          foreach ($params as $field => $value){
              $encoded_params[] = $field . '=' . urlencode($value);
          }
          return $encoded_params;
      }
      
      // Get the response
      function get_response($url) {
          $response = file_get_contents($url);
          // If error, send message back to the client
          if ($response === false) {
              exit("Couldn't get response from the api");
          }
          return $response;
      }
      
      $params = array(
              "part" => "snippet,contentDetails,statistics",
              "id"   => "UC6ltI41W4P14NShIBHU8",
              "key"  => "-----------", // Your API key
      );
      
      $encoded_params = encode_param($params);
      $request_url = "https://www.googleapis.com/youtube/v3/channels?".implode('&', $encoded_params);
      $response = get_response($request_url);
      
      //............
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2011-08-02
      • 2014-02-27
      相关资源
      最近更新 更多