Monday, October 29, 2012

Parse JSON with JQuery

JSON file - json.php

[sourcecode language="php"]
<?php
$json = '{ "students":[

{
"id":"001",
"name" :{"first" : "rahul", "last" : "mani"},
"age" : 20,
"city" : "hydrabad"
},
{
"id":"002",
"name" :{"first" : "deepu", "last" : "patel"},
"age" : 18,
"city" : "delhi"
}
]}';
echo $json;
?>
[/sourcecode]

 


[sourcecode language="html"]
<html>
<head>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var url = "json.php";
$.getJSON(url,function(json){
$.each(json.students,function(i,student){
$("#response").append(
student.id + ' '+ student.name.first + ' ' + student.age + ' ' + student.city + '<br>'
);
});
});
});
</script>
</head>
<body>
<div id="response"></div>
</body>
</html>
[/sourcecode]

Get YouTube Video Comments Using PHP

[sourcecode language="php"]
// video id
$videoId='';
// url where feed is located
$url="http://gdata.youtube.com/feeds/api/videos/{$videoId}/comments";
// get the feed
$comments=simplexml_load_file($url);
// parse the feed using a loop
foreach($comments->entry as $comment)
{
echo '<fieldset>'.$comment->content.'</fieldset>';
}
[/sourcecode]

Tuesday, October 23, 2012

Retrieve Video Details From Youtube

[sourcecode language="html"]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<div>
<form id="youtubeDataQuery" action="#">
<div>
<b>Enter YouTube Video ID or URL in the text box below</b><br/>
<input id="youtubeVideoId" type="text" style="width: 70%;" value="" maxlength="200"/>
<input type="submit" style="width: 28%;" value="Fetch Video Information"/>
</div>
<br/>
<div id="youtubeDataOutput">Video information will appear here.</div>
</form>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
function youtubeDataCallback(data) {
var s = '';
s += '<img src="' + data.entry.media$group.media$thumbnail[0].url + '" width="' + data.entry.media$group.media$thumbnail[0].width + '" height="' + data.entry.media$group.media$thumbnail[0].height + '" alt="' + data.entry.media$group.media$thumbnail[0].yt$name + '" align="right"/>';
s += '<b>Title:</b> ' + data.entry.title.$t + '<br/>';
s += '<b>Author:</b> ' + data.entry.author[0].name.$t + '<br/>';
s += '<b>Published:</b> ' + new Date(data.entry.published.$t).toLocaleDateString() + '<br/>';
s += '<b>Duration:</b> ' + Math.floor(data.entry.media$group.yt$duration.seconds / 60) + ':' + (data.entry.media$group.yt$duration.seconds % 60) + ' (' + data.entry.media$group.yt$duration.seconds + ' seconds)<br/>';
if (data.entry.gd$rating) {
s += '<b>Rating:</b> ' + data.entry.gd$rating.average.toFixed(1) + ' out of ' + data.entry.gd$rating.max + ' (' + data.entry.gd$rating.numRaters + ' ratings)<br/>';
}
s += '<b>Statistics:</b> ' + data.entry.yt$statistics.favoriteCount + ' favorite(s); ' + data.entry.yt$statistics.viewCount + ' view(s)<br/>';
s += '<br/>' + data.entry.media$group.media$description.$t.replace(/\n/g, '<br/>') + '<br/>';
s += '<br/><a href="' + data.entry.media$group.media$player.url + '" target="_blank">Watch on YouTube</a>';
$('#youtubeDataOutput').html(s);
}
$(document).ready(function() {
$('#youtubeDataQuery').submit(function(e) {
e.preventDefault();
var videoid = $('#youtubeVideoId').val();
var m;
if (m = videoid.match(/^http:\/\/www\.youtube\.com\/.*[?&]v=([^&]+)/i) || videoid.match(/^http:\/\/youtu\.be\/([^?]+)/i)) {
videoid = m[1];
}
if (!videoid.match(/^[a-z0-9_-]{11}$/i)) {
alert('Unable to parse Video ID/URL.');
return;
}
$.getScript('http://gdata.youtube.com/feeds/api/videos/' + encodeURIComponent(videoid) + '?v=2&alt=json-in-script&callback=youtubeDataCallback');
});
});
</script>
</div>
</body>
</html>
[/sourcecode]

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...