Parsing YouTube V3 JSON with PHP Example

In this article we’re going over an example of how to parse the JSON output from the YouTube V3 API using PHP.

First you’ll need to login to the Google Developers Console and obtain your API key to use when making queries to the YouTube API.

The following code will execute your query to YouTube and display the title of the video along with a thumbnail and description.

<?

$apikey = “YOURKEY”;
$per_page = 6;
$search = “your search term”;
$category = “2”; //autos

$query = “https://www.googleapis.com/youtube/v3/search?part=snippet&q=$search&maxResults=$per_page&videoCategoryId=$category&safesearch=strict&key=$apikey”;

$json_file3 = file_get_contents(“$query”);

$jfo3 = json_decode($json_file3,true);

foreach($jfo3[‘items’] as $val) {

$title = $val[‘snippet’][‘title’];
$description = $val[‘snippet’][‘description’];
$id = $val[‘id’][‘videoId’];
$thumbnail_url = $val[‘snippet’][‘thumbnails’][‘default’][‘url’];

echo <<<EOF

<p><img width = “250” src = “$thumbnail_url” align = “right”></a>
<a href =”video-viewer.php?v=$id”>$title<BR>
$description</p><br clear=”all”><HR>

EOF;

?>

,


NEW TASER X26 X26E X26C BATTERY DIGITAL POWER MAGAZINE WITH MEMORY CHIP 10-YR SL picture

NEW TASER X26 X26E X26C BATTERY DIGITAL POWER MAGAZINE WITH MEMORY CHIP 10-YR SL

$44.99



Nightingale CXO 6200D Memory Foam, Mesh, headrest , Lumbar Support Office Chair  picture

Nightingale CXO 6200D Memory Foam, Mesh, headrest , Lumbar Support Office Chair

$390.00



Aloudy Ergonomic Memory Foam Office Chair Armrest Pads, Comfy Gaming 2 per Pack picture

Aloudy Ergonomic Memory Foam Office Chair Armrest Pads, Comfy Gaming 2 per Pack

$13.99



32G Voice Activated Mini Spy Digital Sound Audio Recorder Dictaphone MP3 Player picture

32G Voice Activated Mini Spy Digital Sound Audio Recorder Dictaphone MP3 Player

$30.49



Memory Foam Office Chair Armrest Pads With Cooling Gel Gaming Chair Arm Rest picture

Memory Foam Office Chair Armrest Pads With Cooling Gel Gaming Chair Arm Rest

$13.58



Memory Foam Chair Armrest Pads Elbow Pillow Support Arm Rest Covers Office Chair picture

Memory Foam Chair Armrest Pads Elbow Pillow Support Arm Rest Covers Office Chair

$11.59



UDOO BOLT Single Board Computers UDOO BOLT V3 W/ ALUM CASE NO MEMORY picture

UDOO BOLT Single Board Computers UDOO BOLT V3 W/ ALUM CASE NO MEMORY

$239.97



Electric Desk Frame Height Adjustable Standing Single Motor Memory Touch Control picture

Electric Desk Frame Height Adjustable Standing Single Motor Memory Touch Control

$132.99



Mini Hidden Spy Digital Audio Voice Activated Recorder MP3 Playback Small Smart  picture

Mini Hidden Spy Digital Audio Voice Activated Recorder MP3 Playback Small Smart

$23.99



Digital Voice Recorder Dictaphone Audio Mp3 Sound Mini Spy Recorder Mic Lecture picture

Digital Voice Recorder Dictaphone Audio Mp3 Sound Mini Spy Recorder Mic Lecture

$21.75



Powered by WordPress. Designed by WooThemes