-2

Too bad my php knowledge.I'm using YouTube-api.Where will write this code: Retrieve Youtube Channel info for "Vanity" channel

Community
  • 1
  • 1

1 Answers1

0

If you are talking about this line :

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

You are simply making a get request, you can use file_get_contents to get the response for you :

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

Two notes :

  • You have to replace {YOUR_API_KEY} with the developer key. You can easily request one from youtube: http://code.google.com/apis/youtube/dashboard/

  • This is just an example in one line of code, I suggest you use a better approach for making this request like the following :

    // 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);
    
    //............
    
A.Essam
  • 1,094
  • 8
  • 15