-1

I have a small issue that is driving me nuts. Through a GET Request, I obtain a nice amount of json Data from google. How in the world can I loop through it, in order to get only the ['videoID'] entries.

I would be really glad if someone could help me out on this.


$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";


$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // Disable SSL verification
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);

$json = json_decode($result,true);

print_r($json);

1 Answers1

2

Please use below code:

$url ="https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=UUyrB0KeB5pSGPajE0RVMoRA&key=AIzaSyDaLp92MtlcSnJjFVZYjoZIs7z5Wi_P-gQ";


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // Disable SSL verification
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch);
    curl_close($ch);

    $json = json_decode($result,true);

    $videoIds = array();
    foreach($json['items'] as $item){
        $videoIds[] = $item['snippet']['resourceId']['videoId'];
    }
    print_r($videoIds);

Hope this helps.

Jenil Kanani
  • 369
  • 1
  • 14