PHP - Get string values in string

0

I'm new to Wordpress and PHP and I'm having a hard time rsrs

I have a very large string that I get dynamically. This string is actually an array with multiple values and variables. I would like to take all the values contained in the videoID variable that repeats N times within that array (which is actually a string).

Or, in other words, I'm trying to use the YouTube API to show the contents of a playlist on my site. I was just able to get a GIANT string using file_get_contents in url link . If anyone can help me do this:)

Thank you!

    
asked by anonymous 08.12.2016 / 14:21

1 answer

3

I've created an API_KEY to answer your question. :)

The json object that the Youtube API sends is large and has a lot of details, if you want to mount an array with only the ids of the videos, you can do the following:

<?php

$playlist = "SUA_ID_DA_PLAYLIST";
$api_key = "SUA_API_KEY";

// URL com ID da API e da lista de reprodução
$url_do_youtube = "https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId=".$playlist."&key=".$api_key;

$json = file_get_contents($url_do_youtube);

$obj_videos = json_decode($json);

//var_dump($obj_videos);

$lista_videos = $obj_videos->items;

$lista_id_videos = array();

foreach($lista_videos as $obj_video)
{
    $lista_id_videos[] = $obj_video->contentDetails->videoId;
}


var_dump($lista_id_videos);

Hope it helps, if you have any questions, ask me right here. If the answer was helpful, give it a moral and accept it as an answer and click on the little set up to give me reputation points. Falow, Valew. :)

    
08.12.2016 / 15:19