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;

?>

,

CLAROSTAT RV4NAYSD104A 53C3-100K-S POTENTIOMETER 100K OHMS *NEW IN BOX*
$3.99 CLAROSTAT RV4NAYSD104A 53C3-100K-S POTENTIOMETER 100K OHMS *NEW IN BOX* picture
10X K17-02 Bakelite Knobs Hat 18*17*4mm for WH5 WXD3-13 WXD-12 Potentiometer ZS
$1.56 10X K17-02 Bakelite Knobs Hat 18*17*4mm for WH5 WXD3-13 WXD-12 Potentiometer ZS picture
10 x Black Slide Potentiometer Mixer Fader Knob 19mmLx12mmW for 4mm Shaft LA
$2.46 10 x Black Slide Potentiometer Mixer Fader Knob 19mmLx12mmW for 4mm Shaft LA picture
20 pcs (one box) Sfernice/ Vishay PA16NP103MFB15 10k audio potentiometer
$70.0 20 pcs (one box) Sfernice/ Vishay PA16NP103MFB15 10k audio potentiometer picture
DC3V-5V X9C103S Digital Potentiometer Board Module for Arduino 2.7 * 1.3CM
$0.99 DC3V-5V X9C103S Digital Potentiometer Board Module for Arduino  2.7 * 1.3CM picture

Powered by WordPress. Designed by WooThemes