PHP - Get variable values in string

Asked

Viewed 71 times

0

I’m new in Wordpress and PHP and I’m having a little problem rsrs

I have a very large string that I get dynamically. This string is actually an array of various 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 content of a playlist on my site. I could only get a GIANT string using file_get_contents at the url https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId=ID_DA_PLAYLIST&key=API_KEY . If anyone can help me do that :)

Thank you!

  • You cannot encode the json array for easy transfer, and use a foreach to get ids in a new variable?

1 answer

3


I created an API_KEY to answer your question. :)

The json object that the Youtube API sends is big and has a lot of details, if you want to mount an array only with the video ids, 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);

I hope it helps, if you have any questions ask right here. If the answer has been helpful, give a moral and accept as an answer and click the arrow up to give me reputation points. Falow, Valew. :)

  • car****!!!! thanks friend!!! worked perfectly as I needed :D kkk... I am new as "questioner" here at Stackoverflow (was my first question kkk), I am clicking the arrow up but n is incrementing the 0 :s... but I marked as answer kkk Thanks again for the help :D :D

  • Good! Valew my buddy! Good luck there with your project. Hugs. :)

  • @Rbfraphael as soon as you have more points you can vote on the questions and answers, yours and third party.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.