Xach Kaa
Xach Kaa

Reputation: 69

get youtube single video data api and json decode in cakephp 1.3

Ever since Google changed YouTube to API3.0 I can't fetch videos anymore. I can't get video id and api key to apiURL

$video_id = // I stript it from url
$apiKey = Configure::read('YouTube.v3.0.privateKey'); // hidden in bootstrap


$apiURL = 'https://www.googleapis.com/youtube/v3/videos?id=$video_id&key=$apiKey';

and on debug($apiURL); I'm getting

https://www.googleapis.com/youtube/v3/videos?id=$video_id&key=$apiKey

anyone can help? Thanks in Advance.

Upvotes: 0

Views: 244

Answers (2)

Bogdan Kuštan
Bogdan Kuštan

Reputation: 5577

PHP does not evaluate variables in single quotes. Your URL should be places in double quotes:

$apiURL = "https://www.googleapis.com/youtube/v3/videos?id=$video_id&key=$apiKey";

to get variables replaced with values.

Here is good read about outputing text in PHP

Upvotes: 0

Xach Kaa
Xach Kaa

Reputation: 69

if you want little snippet, here we go, it works for me on cakephp1.3 application

$apiURL = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id={$video_id}&key={$apiKey}");

$youtubeResponse = json_decode($apiURL, true);

$youtube_data = $youtubeResponse['items'];
// debug($youtube_data);

$youtube_data_title =  $youtube_data['0']['snippet']['title'];
$youtube_data_description = $youtube_data['0']['snippet']['description'];
$youtube_data_thumbnail = $youtube_data['0']['snippet']['thumbnails']['default']['url'];

    $this->set('youtube_data_title', $youtube_data_title);
    $this->set('youtube_data_description', $youtube_data_description);
    $this->set('youtube_data_thumbnail', $youtube_data_thumbnail);

Upvotes: 1

Related Questions