2016-07-29 5 views
0

Я хотел бы получить название видео с youtube в переменной, но все, что я пробовал, не работает. Часть кода ниже возвращает название фрагмента в переменной $ выхода:Получите название youtube с помощью php-скрипта

{ "элементы": [{ "фрагмент": { "Название": "Hardwell Живите на Ultra Music Festival в Майами 2016 года"}} ]}

Но как я могу получить только заголовок в переменной?

<?php 


function curl_download($Url){ 

    // is cURL installed yet? 
    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    // OK cool - then let's create a new cURL resource handle 
    $ch = curl_init(); 

    // Now set some options (most are optional) 

    // Set URL to download 
    curl_setopt($ch, CURLOPT_URL, $Url); 


    // Include header in result? (0 = yes, 1 = no) 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    // Should cURL return or print out the data? (true = return, false = print) 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Timeout in seconds 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    // Download the given URL, and return output 
    $output = curl_exec($ch); 

    // Close the cURL resource, and free system resources 
    curl_close($ch); 

    return $output; 
} 

$response = curl_download('https://www.googleapis.com/youtube/v3/videos?id=m1ssAFzaCsU&key=AIzaSyBj5GoJlQ4XzebaG6H2tp_WVuQ03JEOOss&fields=items(snippet(title))&part=snippet'); 

if ($response) { 

    $xml = new SimpleXMLElement($response); 
    $title = (string) $xml->title; 
    echo $title; 

} else { 

    // Error handling. 

       echo 'error'; 
} 

?> 
+0

Что эхо $ названия "возвращает в настоящее время? – Ionut

+0

Это не XML, а JSON. – GuyT

+0

Он ничего не возвращает. – Dave

ответ

1

$output является строкой JSON, используйте json_decode разобрать его:

$output = '{ "items": [ { "snippet": { "title": "Hardwell Live at Ultra Music Festival Miami 2016" } } ] }'; 
$output_decoded = json_decode($output); 
$title = $output_decoded->items[0]->snippet->title; 
// $title is now 'Hardwell Live at Ultra Music Festival Miami 2016'; 

адаптированным для вашего кода:

<?php 


function curl_download($Url){ 

    // is cURL installed yet? 
    if (!function_exists('curl_init')){ 
     die('Sorry cURL is not installed!'); 
    } 

    // OK cool - then let's create a new cURL resource handle 
    $ch = curl_init(); 

    // Now set some options (most are optional) 

    // Set URL to download 
    curl_setopt($ch, CURLOPT_URL, $Url); 


    // Include header in result? (0 = yes, 1 = no) 
    curl_setopt($ch, CURLOPT_HEADER, 0); 

    // Should cURL return or print out the data? (true = return, false = print) 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    // Timeout in seconds 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 

    // Download the given URL, and return output 
    $output = curl_exec($ch); 

    // Close the cURL resource, and free system resources 
    curl_close($ch); 

    return $output; 
} 

$response = curl_download('https://www.googleapis.com/youtube/v3/videos?id=m1ssAFzaCsU&key=AIzaSyBj5GoJlQ4XzebaG6H2tp_WVuQ03JEOOss&fields=items(snippet(title))&part=snippet'); 

if ($response) { 

    $response_decoded = json_decode($response); 
    $title = $response_decoded->items[0]->snippet->title; 
    echo $title; 

} else { 

    // Error handling. 

       echo 'error'; 
} 

?> 
+0

Прохладный! Это сделал трюк! Благодаря... – Dave