Get the contacts list from Yahoo Account

Posted: March 8, 2014 in Must Watch, Php
Tags: , ,

Courtesy:-http://www.idiotminds.com/get-the-contacts-list-from-yahoo-account/

 

Now a days,website s are widely using the open invite system .Here,We used Yahoo Oauth for getting the contacts list from yahoo account using Yahoo PHP SDK.Contacts are relationships that exist in Yahoo! address book, such as those entered from across the Yahoo! Network like Yahoo! Mail and Yahoo! Messenger. The API provides read and write access.
Before starting with the Yahoo PHP SDK, you will need to register a web application and you will get web-based application ID,OAuth consumer key and secret. These keys will provide you with general access to the Yahoo! Social Platform, and allow your users to authorize your application to access protected resources and information.

 

Live DemoLive Demo

Download Script

For Creating the Yahoo App,Please Click Here.

In Yahoo My projects page,Click on the New Project Button.It will appears a popup.In that popup,select the Standard option.

Yahoo Application Type

In that Popup click on Continue button after selection of the Yahoo Application type,you will get the Application form.

Yahoo Application Form

After filling the all fields,Click on the Get API Key.You will get application ID,OAuth consumer key and secret.

Yahoo Authentication Information

After that you need to set the permissions for this app.In the Permissions,You need to select the Read Public option under Social Directory (Profiles) of the Social Directory and after you need to click on Save and Change Consumer Key.

Yahoo Contacts

Here is the index.php file.In that file you need to define the OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID

  1. <?php
  2. // Include the YOS library.
  3. require dirname(__FILE__).’/lib/Yahoo.inc’;
  4. //for converting xml to array
  5. function XmltoArray($xml) {
  6.         $array = json_decode(json_encode($xml), TRUE);
  7.         foreach ( array_slice($array, 0) as $key => $value ) {
  8.             if ( empty($value) ) $array[$key] = NULL;
  9.             elseif ( is_array($value) ) $array[$key] = XmltoArray($value);
  10.         }
  11.         return $array;
  12.     }
  13. YahooLogger::setDebug(true);
  14. YahooLogger::setDebugDestination(‘LOG’);
  15. // use memcache to store oauth credentials via php native sessions
  16. //ini_set(‘session.save_handler’, ‘files’);
  17. //session_save_path(‘/tmp/’);
  18. session_start();
  19. // Make sure you obtain application keys before continuing by visiting:
  20. // https://developer.yahoo.com/dashboard/createKey.html
  21. define(‘OAUTH_CONSUMER_KEY’, ‘Your App Consumer Key’);
  22. define(‘OAUTH_CONSUMER_SECRET’, ‘Your App Consumer Secret’);
  23. define(‘OAUTH_DOMAIN’, ‘Your Domain’);
  24. define(‘OAUTH_APP_ID’, ‘Your App Id’);
  25. if(array_key_exists(“logout”, $_GET)) {
  26.   // if a session exists and the logout flag is detected
  27.   // clear the session tokens and reload the page.
  28.   YahooSession::clearSession();
  29.   header(“Location: index.php”);
  30. }
  31. // check for the existance of a session.
  32. // this will determine if we need to show a pop-up and fetch the auth url,
  33. // or fetch the user’s social data.
  34. $hasSession = YahooSession::hasSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
  35. if($hasSession == FALSE) {
  36.   // create the callback url,
  37.   $callback = YahooUtil::current_url().”?in_popup”;
  38. $sessionStore = new NativeSessionStore();
  39.   // pass the credentials to get an auth url.
  40.   // this URL will be used for the pop-up.
  41.   $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback, $sessionStore);
  42. }
  43. else {
  44.   // pass the credentials to initiate a session
  45.   $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
  46.   // if the in_popup flag is detected,
  47.   // the pop-up has loaded the callback_url and we can close this window.
  48.   if(array_key_exists(“in_popup”, $_GET)) {
  49.     close_popup();
  50.     exit;
  51.   }
  52.   // if a session is initialized, fetch the user’s profile information
  53.   if($session) {
  54.     // Get the currently sessioned user.
  55.     $user = $session->getSessionedUser();
  56.     // Load the profile for the current user.
  57.     $profile = $user->getProfile();
  58.     $profile_contacts=XmltoArray($user->getContactSync());
  59.    $contacts=array();
  60.    foreach($profile_contacts[‘contactsync’][‘contacts’] as $key=>$profileContact){
  61.        foreach($profileContact[‘fields’] as $contact){
  62.           $contacts[$key][$contact[‘type’]]=$contact[‘value’];
  63.        }
  64.    }
  65.   }
  66. }
  67. /**
  68.  * Helper method to close the pop-up window via javascript.
  69.  */
  70. function close_popup() {
  71. ?>
  72. <script type=”text/javascript”>
  73.   window.close();
  74. </script>
  75. <?php
  76. }
  77. ?>
  78. <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01//EN”>
  79. <html>
  80.   <head>
  81.     <title>Get the contact list from Yahoo-Idiot Minds</title>
  82. <style type=”text/css”>
  83. body{
  84. background-color: #F2F2F2;
  85. }
  86. .yh_frnds{
  87.     list-style:none;
  88. }
  89. .yh_frnds li{
  90.     padding:10px;
  91.     float:left;
  92.     width:30%;
  93. }
  94. .frnd_list{
  95.     margin-top:-25px;
  96.     margin-left:40px;
  97. }
  98. .fb_frnds a{
  99.         text-decoration:none;
  100.          background: #333;
  101.          filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=’#333′, endColorstr=’#D95858′); /* for IE */
  102. background: -webkit-gradient(linear, left top, left bottom, from(#333), to(#D95858)); /* for webkit browsers */
  103. background: -moz-linear-gradient(top,  #333,  #D95858)/* for firefox 3.6+ */ ;
  104.     color: #FFFFFF;
  105.         float: right;
  106.         font: bold 13px arial;
  107.         margin-right:110px ;
  108. }
  109. </style>
  110.     <!– Combo-handled YUI JS files: –>
  111.     <script type=”text/javascript” src=”yahoo-dom-event.js”></script>
  112.     <script type=”text/javascript” src=”popupmanager.js”></script>
  113.   </head>
  114.   <body>
  115.     <?php
  116.       if($hasSession == FALSE) {
  117.         // if a session does not exist, output the
  118.         // login / share button linked to the auth_url.
  119.         ?>
  120.        <a href=”<?php echo $auth_url; ?>” id=”yloginLink”><img src=”yahoo-oauth-connect.png” style=”width:150px;margin-left: 350px;” title=”Get the contact list from Yahoo-Idiot Minds” alt=”Get the contact list from Yahoo-Idiot Minds” /></a>
  121.     <?php  }
  122.       else if($hasSession && $profile) {
  123.         // if a session does exist and the profile data was
  124.         // fetched without error, print out a simple usercard.
  125.        ?>
  126.       <img src=”<?php echo $profile->image->imageUrl; ?>” style=”width:10%;” />
  127.       <h2>Hi <a href=”<?php echo $profile->profileUrl; ?>” target=”_blank”><?php echo $profile->nickname; ?></a>
  128.       </h2>
  129. <?php
  130.         if($profile->status->message != “”) {
  131.           $statusDate = date(‘F j, y, g:i a’, strtotime($profile->status->lastStatusModified));
  132.           echo sprintf(“<p><strong>“</strong>%s<strong>”</strong> on %s</p>”, $profile->status->message, $statusDate);
  133.         }
  134.         echo “<p><a href=\”?logout\”>Logout</a></p>”; ?>
  135.         <ul >
  136. <?php
  137.     foreach($contacts as $user_friend){
  138.      if(isset($user_friend[’email’])){
  139. ?>
  140. <li ><img src=”yahoo1.png” width=”30″ height=”30″/>
  141. <div ><strong><?php echo $user_friend[‘name’][‘givenName’]; ?></strong><br /><?php echo $user_friend[’email’];?></div>
  142. </li>
  143. <?php }
  144.  }  ?>
  145. </ul>
  146.  <?php     }
  147.     ?>
  148.     <script type=”text/javascript”>
  149.       var Event = YAHOO.util.Event;
  150.       var _gel = function(el) {return document.getElementById(el)};
  151.       function handleDOMReady() {
  152.         if(_gel(“yloginLink”)) {
  153.           Event.addListener(“yloginLink”, “click”, handleLoginClick);
  154.         }
  155.       }
  156.       function handleLoginClick(event) {
  157.         // block the url from opening like normal
  158.         Event.preventDefault(event);
  159.         // open pop-up using the auth_url
  160.         var auth_url = _gel(“yloginLink”).href;
  161.         PopupManager.open(auth_url,600,435);
  162.       }
  163.       Event.onDOMReady(handleDOMReady);
  164.     </script>
  165.   </body>
  166. </html>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s