Thursday, December 1, 2016

Youtube API Searching By Name and Customizing the Details Obtained!


<div  align="center">
  <body>
    <form method="GET">
  <table>
  <tr><td>Search Term: <input type="text" id="q" name="q" placeholder="Enter Search Term"></td></tr>
  <tr><td> Max Results: <input type="number" id="maxResults" name="maxResults"  step="1" placeholder="Enter Search No."></td></tr>

  <tr><td> <input type="submit" value="Search"></td></tr>
  </table>
</form>
<h3>Videos</h3>
</body>
</div>





<?php
/**********************************************************************************/
?>




<script>
function detail(i){
var lngdscp=document.getElementById("discplg"+i).innerHTML;
var stdscp=document.getElementById("discpst"+i).innerHTML;

var x =document.getElementById("descp"+i).innerHTML;

  if(x=="More&gt;&gt;"){
 x=document.getElementById("descp"+i).innerHTML="Less&lt;&lt;";
  document.getElementById("discp"+i).innerHTML=lngdscp;
  }
  else{
      x=document.getElementById("descp"+i).innerHTML="More&gt;&gt;";
 document.getElementById("discp"+i).innerHTML=stdscp;
  }
}
function abc(x){
document.getElementById("myframe").src="https://www.youtube.com/embed/"+x+"?showinfo=0";
}
</script>




<?php
/**********************************************************************************/


$apikey ='Wamyyyjjjj00000mutguiopred90mmmopred'; //replace it with your own
if(isset($_GET['q'])){
$searchTerm=urlencode($_GET['q']);
}
else{
$searchTerm="abcd";
}


if(isset($_GET['maxResults'])){
$maxResults=$_GET['maxResults'];
}
else{
$maxResults=5;
}
$maxResults;
$url = "https://www.googleapis.com/youtube/v3/search?part=id&maxResults=10&q=".$searchTerm."&type=video&key=".$apikey;
$ch=curl_init($url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$body=curl_exec($ch);
curl_close($ch);

$data=json_decode($body,true);
echo "<div align='center'>";
echo"<table boder='1'>";

echo"<tr><td colspan='2' align='center'>";
$videoid1= $data['items'][0]['id']['videoId'];
echo'<iframe id="myframe" width="560" height="315" src="https://www.youtube.com/embed/'.$videoid1.'?showinfo=0" frameborder="0" allowfullscreen></iframe>';
echo"</td></tr>";
$output='';
for($i=0; $i<$maxResults; $i++){
$videoid=$data['items'][$i]['id']['videoId'];

$url2 = "https://www.googleapis.com/youtube/v3/videos?id=".$videoid."&key=".$apikey."&part=snippet,statistics,status&fields=items(snippet(publishedAt,title,channelTitle,description,thumbnails(default(url))),status(privacyStatus),statistics(viewCount,likeCount,dislikeCount,commentCount))";

//Fetching the data using CURL
$ch=curl_init($url2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$body2=curl_exec($ch);
curl_close($ch);

$data2=json_decode($body2,true);


//Displaying the result
echo'<tr><td><a href="javascript:void(0)" onclick="abc(\''.$videoid.'\')"><img src="' . $data2['items'][0]['snippet']['thumbnails']['default']['url'] . '" alt="error"></a></td></tr>';

echo "<tr><td width='500'><strong><font color=red>Channel Title:</font></strong>".$data2['items'][0]['snippet']['channelTitle']."</td></tr>";

echo"<tr><td width='500'><strong><font color=red>Title:</font></strong>".$data2['items'][0]['snippet']['title']." ".'<br> <strong><font color=red>Date Published:</font></strong>'.$data2['items'][0]['snippet']['publishedAt']."</td></tr>";
echo "<tr><td width='500'><strong><font color=red>No.of view:</font></strong>".$data2['items'][0]['statistics']['viewCount']."</td></tr>";
echo "<tr><td width='500'><strong><font color=red>No.of like:</font></strong>".$data2['items'][0]['statistics']['likeCount']."</td></tr>";
echo "<tr><td width='500'><strong><font color=red>No.of Dislike:</font></strong>".$data2['items'][0]['statistics']['dislikeCount']."</td></tr>";
echo "<tr><td width='500'><strong><font color=red>No.of Comment:</font></strong>".$data2['items'][0]['statistics']['commentCount']."</td></tr>";
echo "<tr><td width='500'><strong><font color=red>Privacy Status:</font></strong>".$data2['items'][0]['status']['privacyStatus']."</td></tr>";

$description=$data2['items'][0]['snippet']['description'];
$myarray=array();
$myarray=explode(" ",$description);
count($myarray);
if(count($myarray)>10){
$num=10;
}
else{
$num=count($myarray);
}
$st="";

   for($j=0;$j<$num;$j++){
   $st.=$myarray[$j];
   $st.=" ";
   }

echo"<div id='discplg".$i."' style='display:none'>".$description."</div>";
echo "<div id='discpst".$i."' style='display:none'>".$st."</div>";



echo "<tr><td width='500'><strong><font color=red>Description:</font></strong><div id='discp".$i."'>".$st."</div><a id='descp".$i."' href='javascript:void(0)' onclick='detail($i)'>More>></a></div></td></tr>";



}
echo"</table>";
echo "</div>";

?>




No comments:

Post a Comment