Im trying to extract the title and description of a youTube video using a php file.
So far, i was able to create the url and paste into chrome browser and i got the expected results.
Now, i want to do the same thing using a PHP file called from ajax. But, i get nothing.
Heres what i've done so far:
html
<span class="icon iDocument" data-do='{"do":"getYoutube", "path":"xxx"}'</span>
ajax.js
$("[data-do]").click(function(event) {
var params = $(this).data("do");
var action = params['do'];
switch (action) {
case "getYoutube":
script = "/admin/include/action/get_youtube.php";
$.ajax({
type: "POST",
url: script,
data: params
}).done(function(data) {
alert(data);
}).fail(function() {
alert("Error.");
}).always(function(data) {
})
break;
default:
break;
}
});
get_youtube.php
<?php
if ( isset($_POST['path']) && trim($_POST['path']) != '' ) {
$get_path = trim($_POST['path']); // for use later
// work OK if paste in a browser (vars are harcoded for testing purposes)
$url ="https://www.googleapis.com/youtube/v3/videos/?key=keygoeshere&id=videoidgoeshere&part=snippet";
$video = json_decode($url, true);
$test = $video['kind']; // for testing purposes
echo $test;
} else {
echo 'Video path not found';
}
?>
Im supposed to get an alert box with some data, but i get an empty alert box?
What am i doing wrong?
$url ="https://www.googleapis.com/youtube/v3/videos/?key=keygoeshere&id=videoidgoeshere&part=snippet";
$video = json_decode($url, true);
Should be
$url = @file_get_contents("https://www.googleapis.com/youtube/v3/videos/?key=keygoeshere&id=videoidgoeshere&part=snippet");
$video = json_decode($url, true);