Thursday, 25 April 2013

How to validate the Multiple Emails Using Jquery validation


$.validator.addMethod(
    "multiemail",
     function(value, element) {
         if (this.optional(element)) // return true on optional element
             return true;
         var emails = value.split(/[,]+/); // split element by , and ;
         valid = true;
         for (var i in emails) {
             value = emails[i];
             valid = valid &&
                     jQuery.validator.methods.email.call(this, $.trim(value), element);
         }
         return valid;
     },

    jQuery.validator.messages.email
);

Saturday, 20 April 2013

How to get the Youtube video information using id in php


<?php
    // function to parse a video <entry>
    function parseVideoEntry($entry) {    
      $obj= new stdClass;
   
      // get nodes in media: namespace for media information
      $media = $entry->children('http://search.yahoo.com/mrss/');
      $obj->title = $media->group->title;
      $obj->description = $media->group->description;
   
      // get video player URL
      $attrs = $media->group->player->attributes();
      $obj->watchURL = $attrs['url'];
   
      // get video thumbnail
      $attrs = $media->group->thumbnail[0]->attributes();
      $obj->thumbnailURL = $attrs['url'];
         
      // get <yt:duration> node for video length
      $yt = $media->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->duration->attributes();
      $obj->length = $attrs['seconds'];
   
      // get <yt:stats> node for viewer statistics
      $yt = $entry->children('http://gdata.youtube.com/schemas/2007');
      $attrs = $yt->statistics->attributes();
      $obj->viewCount = $attrs['viewCount'];
   
      // get <gd:rating> node for video ratings
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->rating) {
        $attrs = $gd->rating->attributes();
        $obj->rating = $attrs['average'];
      } else {
        $obj->rating = 0;      
      }
     
      // get <gd:comments> node for video comments
      $gd = $entry->children('http://schemas.google.com/g/2005');
      if ($gd->comments->feedLink) {
        $attrs = $gd->comments->feedLink->attributes();
        $obj->commentsURL = $attrs['href'];
        $obj->commentsCount = $attrs['countHint'];
      }
   
      // get feed URL for video responses
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
      2007#video.responses']");
      if (count($nodeset) > 0) {
        $obj->responsesURL = $nodeset[0]['href'];    
      }
       
      // get feed URL for related videos
      $entry->registerXPathNamespace('feed', 'http://www.w3.org/2005/Atom');
      $nodeset = $entry->xpath("feed:link[@rel='http://gdata.youtube.com/schemas/
      2007#video.related']");
      if (count($nodeset) > 0) {
        $obj->relatedURL = $nodeset[0]['href'];    
      }
 
      // return object to caller
      return $obj;    
    }
    $vid='VIDEO ID';
    // set video data feed URL
    $feedURL = 'http://gdata.youtube.com/feeds/api/videos/' . $vid;

    // read feed into SimpleXML object
    $entry = simplexml_load_file($feedURL);
 
    // parse video entry
    $video = parseVideoEntry($entry);
     
    // display main video record
    echo "<table>\n";
    echo "<tr>\n";
    echo "<td><a href=\"{$video->watchURL}\">
    <img src=\"$video->thumbnailURL\"/></a></td>\n";
    echo "<td><a href=\"{$video->watchURL}\">{$video->title}</a>
    <br/>\n";
    echo sprintf("%0.2f", $video->length/60) . " min. | {$video->rating}
    user rating | {$video->viewCount} views<br/>\n";
echo 'desc';
    echo $video->description . " end </td>\n";
    echo "</tr>\n";
 
    // read 'video comments' feed into SimpleXML object
    // parse and display each comment
    if ($video->commentsURL && $video->commentsCount > 0) {
      $commentsFeed = simplexml_load_file($video->commentsURL);  
      echo "<tr><td colspan=\"2\"><h3>" . $commentsFeed->title .
      "</h3></td></tr>\n";
      echo "<tr><td colspan=\"2\"><ol>\n";
      foreach ($commentsFeed->entry as $comment) {
        echo "<li>" . $comment->content . "</li>\n";
      }
      echo "</ol></td></tr>\n";
    }
 
    // read 'video responses' feed into SimpleXML object
    // parse and display each video entry
    if ($video->responsesURL) {
      $responseFeed = simplexml_load_file($video->responsesURL);  
      echo "<tr><td colspan=\"2\"><h3>" .
      $responseFeed->title . "</h3></td></tr>\n";
      foreach ($responseFeed->entry as $response) {
        $responseVideo = parseVideoEntry($response);
        echo "<tr>\n";
        echo "<td><a href=\"{$responseVideo->watchURL}\">
        <img src=\"$responseVideo->thumbnailURL\"/></a></td>\n";
        echo "<td><a href=\"{$responseVideo->watchURL}\"
        >{$responseVideo->title}</a><br/>\n";
        echo sprintf("%0.2f", $responseVideo->length/60) . " min. |
        {$responseVideo->rating} user rating | {$responseVideo->viewCount}
        views<br/>\n";
        echo $responseVideo->description . "</td>\n";
        echo "</tr>\n";    
      }
    }
 
    // read 'related videos' feed into SimpleXML object
    // parse and display each video entry
    if ($video->relatedURL) {
      $relatedFeed = simplexml_load_file($video->relatedURL);  
      echo "<tr><td colspan=\"2\"><h3>" .
      $relatedFeed->title . "</h3></td></tr>\n";
      foreach ($relatedFeed->entry as $related) {
        $relatedVideo = parseVideoEntry($related);
        echo "<tr>\n";
        echo "<td><a href=\"{$relatedVideo->watchURL}\">
        <img src=\"$relatedVideo->thumbnailURL\"/></a></td>\n";
        echo "<td><a href=\"{$relatedVideo->watchURL}\">
        {$relatedVideo->title}</a><br/>\n";
        echo sprintf("%0.2f", $relatedVideo->length/60) . " min. |
        {$relatedVideo->rating} user rating | {$relatedVideo->viewCount}
        views<br/>\n";
        echo $relatedVideo->description . "</td>\n";
        echo "</tr>\n";    
      }
    }
    echo "</table>\n";  
    ?>

Get the Gmail Contacts using PHP



error_reporting (E_ALL);

$user = 'YOUR EMAIL'
$password ='YOUR PASSWORD';


// step 1: login
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array (
'Email' => $user,
'Passwd' => $password,
'service' => 'cp', // <== contact list service code
'source' => 'test-google-contact-grabber',
'accountType' => 'GOOGLE',
);

$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL,$login_url);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);

$returns = array ();

foreach (explode ("\n",$result) as $line)
{
$line = trim ($line);
if (!$line) continue;
list ($k,$v) = explode ("=",$line,2);

$returns[$k] = $v;
}

curl_close ($curl);

// step 2: grab the contact list
$feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";

$header = array (
'Authorization: GoogleLogin auth=' . @$returns['Auth'],
);

$curl = curl_init ();
curl_setopt ($curl, CURLOPT_URL, $feed_url);
curl_setopt ($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec ($curl);
curl_close ($curl);

$data = json_decode ($result);

$contacts = array ();

//NOW CHECK IF THE ENTRY IS EXIST OR NOT
if(isset($data->feed->entry)){
foreach ($data->feed->entry as $entry)
{
$contact = new stdClass();
//$contact->title = $entry->title->{'$t'};
if(isset($entry->{'gd$email'}[0]->address)){
$contact->email = $entry->{'gd$email'}[0]->address;

$contacts[] = $contact;}
}
     echo '<pre>';
 print_r($contacts);
 echo '</pre>';exit;

 }else
 //show the error
print_r($data->feed);