2016-07-29 17 views
0

Ich möchte den Titel eines YouTube-Videos in einer Variablen, aber alles, was ich versucht habe, hat nicht funktioniert. Ein Teil des Codes unten gibt den Titel Schnipsel in Variable $ output:Holen Sie Youtube Titel mit PHP-Skript

{ "Artikel": [{ "Schnipsel": { "title": "Hardwell Live at Ultra Music Festival Miami 2016"}} ]}

Aber wie konnte ich nur den Titel in einer Variablen bekommen?

<?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

Was echo $ title 'jetzt zurückkehrt? – Ionut

+0

Es ist kein XML, sondern JSON. – GuyT

+0

Es gibt nichts zurück. – Dave

Antwort

1

$output ist ein JSON-String verwenden json_decode es zu analysieren:

$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'; 

für Ihren Code Angepasst:

<?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

Cool! Das hat den Trick gemacht! Vielen Dank... – Dave