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;

?>

,

Lenovo DCG Dcg 4xg7a07195 Xeon Silver 4110 Proc Opt Kit
$720.9 Lenovo DCG Dcg 4xg7a07195 Xeon Silver 4110 Proc Opt Kit picture
HP Proliant ML110 G6 - Intel Xeon Quad Core - X3430 @ 2.40GHz 4GB PC3 P212~
$261.14 HP Proliant ML110 G6 - Intel Xeon Quad Core - X3430 @ 2.40GHz 4GB PC3 P212~ picture
Pelco DSSRV2-200 DVR w/Intel Quad Xeon E3-1275 V3 3.50Ghz/8Gb RAM/6 Caddies noHD
$499.99 Pelco DSSRV2-200 DVR w/Intel Quad Xeon E3-1275 V3 3.50Ghz/8Gb RAM/6 Caddies noHD picture
INTEL PENTIUM III XEON PROCESSOR 500/100/1M S2 KIT-NEW -NOS
$4.95 INTEL PENTIUM III  XEON PROCESSOR 500/100/1M S2 KIT-NEW -NOS picture
NEW GENUINE FOXCONN Motherboard socket LGA2011 XEON CPU Protector Cover
$1.99 NEW GENUINE FOXCONN Motherboard socket LGA2011 XEON CPU Protector Cover picture

Powered by WordPress. Designed by WooThemes