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

// Include the YOS library.
require dirname(__FILE__).'/lib/';
//for converting xml to array
function XmltoArray($xml) {
        $array = json_decode(json_encode($xml), TRUE);

        foreach ( array_slice($array, 0) as $key => $value ) {
            if ( empty($value) ) $array[$key] = NULL;
            elseif ( is_array($value) ) $array[$key] = XmltoArray($value);
        return $array;

// use memcache to store oauth credentials via php native sessions
//ini_set('session.save_handler', 'files');
// Make sure you obtain application keys before continuing by visiting:
define('OAUTH_CONSUMER_KEY', 'Your App Consumer Key');
define('OAUTH_CONSUMER_SECRET', 'Your App Consumer Secret');
define('OAUTH_DOMAIN', 'Your Domain');
define('OAUTH_APP_ID', 'Your App Id');
if(array_key_exists("logout", $_GET)) {
  // if a session exists and the logout flag is detected
  // clear the session tokens and reload the page.
  header("Location: index.php");
// check for the existance of a session.
// this will determine if we need to show a pop-up and fetch the auth url,
// or fetch the user's social data.

if($hasSession == FALSE) {
  // create the callback url,
  $callback = YahooUtil::current_url()."?in_popup";
$sessionStore = new NativeSessionStore();
  // pass the credentials to get an auth url.
  // this URL will be used for the pop-up.
  $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback, $sessionStore);

else {
  // pass the credentials to initiate a session
  $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

  // if the in_popup flag is detected,
  // the pop-up has loaded the callback_url and we can close this window.
  if(array_key_exists("in_popup", $_GET)) {
  // if a session is initialized, fetch the user's profile information
  if($session) {
    // Get the currently sessioned user.
    $user = $session->getSessionedUser();

    // Load the profile for the current user.
    $profile = $user->getProfile();
   foreach($profile_contacts['contactsync']['contacts'] as $key=>$profileContact){
       foreach($profileContact['fields'] as $contact){

 * Helper method to close the pop-up window via javascript.
function close_popup() {
<script type="text/javascript">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <title>Get the contact list from Yahoo-Idiot Minds</title>
<style type="text/css">
background-color: #F2F2F2;
.yh_frnds li{
.fb_frnds a{
         background: #333;
         filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#333', endColorstr='#D95858'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#333), to(#D95858)); /* for webkit browsers */
background: -moz-linear-gradient(top,  #333,  #D95858)/* for firefox 3.6+ */ ;
    color: #FFFFFF;
        float: right;
        font: bold 13px arial;
        margin-right:110px ;

    <!-- Combo-handled YUI JS files: -->
    <script type="text/javascript" src="yahoo-dom-event.js"></script>
    <script type="text/javascript" src="popupmanager.js"></script>

      if($hasSession == FALSE) { 
        // if a session does not exist, output the
        // login / share button linked to the auth_url.
       <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>
    <?php  }
      else if($hasSession && $profile) {  

        // if a session does exist and the profile data was
        // fetched without error, print out a simple usercard.
      <img src="<?php echo $profile->image->imageUrl; ?>" style="width:10%;" />
      <h2>Hi <a href="<?php echo $profile->profileUrl; ?>" target="_blank"><?php echo $profile->nickname; ?></a>

        if($profile->status->message != "") {
          $statusDate = date('F j, y, g:i a', strtotime($profile->status->lastStatusModified));
          echo sprintf("<p><strong>&#8220;</strong>%s<strong>&#8221;</strong> on %s</p>", $profile->status->message, $statusDate);

        echo "<p><a href=\"?logout\">Logout</a></p>"; ?>

        <ul >
    foreach($contacts as $user_friend){

<li ><img src="yahoo1.png" width="30" height="30"/>
<div ><strong><?php echo $user_friend['name']['givenName']; ?></strong><br /><?php echo $user_friend['email'];?></div>

<?php }
 }  ?>
 <?php     }
    <script type="text/javascript">
      var Event = YAHOO.util.Event;
      var _gel = function(el) {return document.getElementById(el)};

      function handleDOMReady() {
        if(_gel("yloginLink")) {
          Event.addListener("yloginLink", "click", handleLoginClick);
      function handleLoginClick(event) {
        // block the url from opening like normal

        // open pop-up using the auth_url
        var auth_url = _gel("yloginLink").href;,600,435);


  1. Great Work Able to access contact!!
    Thanks a lot!

  2. What should i put in OAUTH_DOMAIN if i am working in localhost?

  3. Great Work ..!

  4. I tried this but its not working :(
    I defined OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID correctly but still it’s not working properly.

  5. Hi,

    I am following your tutorial BUT the $auth_url returns NULL value. Please help me to get rid of this.


    • For getting the $auth_url,you must define the valid OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_DOMAIN, OAUTH_APP_ID values of the Yahoo app in index.php.And Yahoo apps are not work in localhost.

  6. tejas jahdav

    bro thank u…………….. its working for me i try

  7. Daine Dunker

    Hiya. Fantastically nice website!! Guy .. Striking .. Superb .. I’ll bookmark your website and assume the feeds also…I’m happy to locate numerous cooperative in a row here in the article. Be grateful you for sharing..

Leave a Reply