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;

?>

,

Supermicro X6DHT-G Dual Xeon Server Motherboard w/ Adaptec AAR-2020SA
$199.99 Supermicro X6DHT-G Dual Xeon Server Motherboard w/ Adaptec AAR-2020SA  picture
2Pcs Ch341a Programmer Usb Motherboard Routing Bios Lcd Flash 2425 Burner New D
$7.7 2Pcs Ch341a Programmer Usb Motherboard Routing Bios Lcd Flash 2425 Burner New D picture
Used Motherboard for MSI P432124 C34 Socket 75 DDR2 4GB USB2 For Parts
$221.0 Used Motherboard for MSI P432124 C34 Socket 75 DDR2 4GB USB2 For Parts picture
Motherboard Power Momentary Push Button Switch Cable Lead 45cm 100 Pcs
$34.65 Motherboard Power Momentary Push Button Switch Cable Lead 45cm 100 Pcs picture
uxcell Anti Static ESD Cleaning Brush for PCB Motherboards Fans Keyboards
$9.09 uxcell Anti Static ESD Cleaning Brush for PCB Motherboards Fans Keyboards picture

Powered by WordPress. Designed by WooThemes