Estoy tratando de extraer el título y la descripción de un video de YouTube usando un archivo php.
Hasta ahora, pude crear la URL y pegarla en el navegador Chrome y obtuve los resultados esperados.
Ahora, quiero hacer lo mismo usando un archivo PHP llamado desde ajax. Pero, no consigo nada.
Esto es lo que he hecho hasta ahora:
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'; } ?>Se supone que debo obtener un cuadro de alerta con algunos datos, pero aparece un cuadro de alerta vacío.
¿Qué estoy haciendo mal?
$url ="https://www.googleapis.com/youtube/v3/videos/?key=keygoeshere&id=videoidgoeshere&part=snippet"; $video = json_decode($url, true);Debiera ser
$url = @file_get_contents("https://www.googleapis.com/youtube/v3/videos/?key=keygoeshere&id=videoidgoeshere&part=snippet"); $video = json_decode($url, true);