In this blog post, We’ll learn that how we can export yahoo contacts with the help of a PHP Program. We’re going to create an application which will export contacts in a CSV file, i.e, excel sheet.
Yahoo uses OAuth authentication protocol to grant access to user data.So we’ll use the same library to achieve this purpose for our application.
User will get prompt to sign in followed by redirection to the Yahoo login page followed by the Yahoo Contacts display page.
So we’ll see the complete process step-by-step.


Export Yahoo Contacts Using PHP Demo


For a quick and complete solution, you can use our email marketing service MailGet.


Files to be needed:

First of all you need to download Yahoo Oauth Library. You can download it from github. It contains an example along with the library files.


Process:

Step 1:

In the first step, you need to sign-up to get a Consumer Key for the application. This key is application specific, not user specific. If you want to register your application and wish to obtain a Consumer key, Click here. Login with your yahoo credentials when asked. Click on Create an App and follow the process explained in the following image.

Create Application

Yahoo will provide you a Consumer Key and a Consumer Secret.


Step 2:

In this step, we’ll use get_request_token() method to get a Request Token from our application. Yahoo will response with a request token (OAuth token) along with request token secret (OAuth Token Secret).

First of all you need to make changes in the library file globals.php. You need to paste OAUTH_CONSUMER_KEY & OAUTH_CONSUMER_SECRET, you just generated in the previous step.

define('OAUTH_CONSUMER_KEY', 'YOUR CONSUMER KEY');
define('OAUTH_CONSUMER_SECRET', 'YOUR CONSUMER SECRET');

So, create a PHP file named index.php.
In this file, we’re going to define callback URL and obtain request token along with the request token secret.

Given below is the callback URL.

$callback = "https://www.formget.com/tutorial/export-yahoo-contacts-using-php/yahoo_callback.php";

Now, get_request_token()

get_request_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback, false, true, true);

There will be other code including headers used for the parsing body to extract the request token and secret from the result returned by get_request_token. The user will be redirected to the callback domain.


Step 3:

After the 2nd step, the user will be directed to yahoo for authorization using request_auth. Yahoo then asks user to enter his/her credentials and then the user will able to access private data if he/she is authorized to. After this, authorized user will be directed back to the application.


Step 4:

In this step, request token and OAuth verifier will be exchanged for Access Token with the help of get_access_token_yahoo() method. Yahoo then grants an access token and along with an access token secret.

So, you need to create another PHP file with a name yahoo_callback.php.
In this file, access token will get exchanged.

This function calling is responsible for token exchanging.

 get_access_token_yahoo(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $request_token, $request_token_secret, $oauth_verifier, false, true, true);

Now with the help of callcontact_yahoo() method, we’ll access Yahoo Contacts.

callcontact_yahoo(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $guid, $access_token, $access_token_secret, false, true);

There will be other code including headers used for parsing body to extract the access token and secret from the result returned by get_access_token_yahoo.

So these are the main steps you need to follow. Now take a look at the code given below.


PHP File : index.php

This is the main application file where user will be asked to sign-in.

<?php
require_once('globals.php');
require_once('oauth_helper.php');
// Callback URL
$callback = "https://www.formget.com/tutorial/export-yahoo-contacts-using-php/yahoo_callback.php";
// Get the request token using HTTP GET and HMAC-SHA1 signature
$retarr = get_request_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback, false, true, true);
if (! empty($retarr)){
list($info, $headers, $body, $body_parsed) = $retarr;
if ($info['http_code'] == 200 && !empty($body)) {
$_SESSION['request_token'] = $body_parsed['oauth_token'];
$_SESSION['request_token_secret'] = $body_parsed['oauth_token_secret'];
$_SESSION['oauth_verifier'] = $body_parsed['oauth_token'];
$url = urldecode($body_parsed['xoauth_request_auth_url']);
}
}
?>

<!--HTML Code (Interface) -->
<html>
<head>
<title>Export Yahoo Contacts Using PHP</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container-fluid">
<h1>Export Yahoo Contacts Using PHP</h1>
<div id="login">
<div class="row">
<div class="col-md-12">
<h2 id="h2">Yahoo Sign-in</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<a id="signin" href="<?php echo $url; ?>"><img id="signinwithyahoo" class="img-responsive" src="images/sign-in-with-yahoo.png"></a>
</div>
</div>
</div>
</div>
</body>
</html>

PHP File : yahoo_callback.php

This is the file where user will be redirected after the authorization and where contacts will be displayed.

<?php
require_once('globals.php');
require_once('oauth_helper.php');
// Fill in the next 3 variables.
$request_token = $_SESSION['request_token'];
$request_token_secret = $_SESSION['request_token_secret'];
$oauth_verifier = $_GET['oauth_verifier'];
// Get the access token using HTTP GET and HMAC-SHA1 signature
$retarr = get_access_token_yahoo(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $request_token, $request_token_secret, $oauth_verifier, false, true, true);
if (! empty($retarr)) {
list($info, $headers, $body, $body_parsed) = $retarr;
if ($info['http_code'] == 200 && !empty($body)) {
$guid = $body_parsed['xoauth_yahoo_guid'];
$access_token = rfc3986_decode($body_parsed['oauth_token']) ;
$access_token_secret = $body_parsed['oauth_token_secret'];
// Call Contact API
$retarrs = callcontact_yahoo(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $guid, $access_token, $access_token_secret, false, true);
echo "<pre/>";
}}
?>

<!--HTML Code (Interface) -->
<html>
<head>
<title>Export Yahoo Contacts Using PHP</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script src="js/logout.js" type="text/javascript"></script>
</head>
<body>
<div class="container-fluid">
<h1>Export Yahoo Contacts Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="csv col-md-3"><a href ="https://www.formget.com/tutorial/export-yahoo-contacts-using-php/download.php?<?php echo http_build_query($retarrs); ?>" id="download"><img class="img-responsive" src="images/download-csv-icon.gif"></a></div>
<div class="col-md-6"><h2><span>Yahoo Contacts</span></h2></div>
<div class="col-md-3"><a href ="#" onclick="caller()" id="logout"><img class="img-responsive" src="images/button-power_green.png"></a></div>
</div>
<div class="row">
<div class="col-md-12">
<table cellspacing='0'>
<thead>
<td>Name</td>
<td>Email</td>
</thead>
<?php
foreach ($retarrs as $key => $value) {?>
<tr>
<td><?php echo $value['name']; ?></td>
<td><?php echo $value['email']; ?></td>
</tr>
<?php }
?>
</table>
</div>
</div>
</div>
</div>
</body>
</html>

PHP File : download.php

This file is responsible for the download of CSV file.

<?php

if(isset($_GET))
{
$retarrs = $_GET;
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=Contacts.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen('php://output', 'w');
fputcsv($file, array('Name', 'Email'));
foreach($retarrs as $key => $value) {
$data['name'] = $value['name'];
$data['email'] = $value['email'];
fputcsv($file, $data);
}
exit();
}
?>

JS File : logout.js

function call()
{
popup = window.open('https://login.yahoo.com/config/login?logout=1&.direct=2&.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/');
setTimeout(wait, 4000);
}
function caller()
{
call();
}

function wait()
{
popup.close();
window.location.href = 'https://www.formget.com/tutorial/export-yahoo-contacts-using-php/index.php';
}


CSS File : style.css

This is the css styling code for the index.php file, i.e, our main application Page.

@import url(http://fonts.googleapis.com/css?family=Raleway);
#h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
border-bottom: 1px solid #ccc;
color: black;
font-weight: bolder;
font-size: 2em;
margin: 0px -15px;
padding: 8% 0;
font-family:raleway;
}
.wrap{
width: 33%;
margin: 5% auto;
}
.container-fluid{
width: 44%;
margin: auto auto;
}
#login{
border: 2px solid #ccc;
border-radius: 10px;
font-family:raleway;
}#signinwithyahoo{
margin: auto;
width: 50%;
padding: 6% 0;
}
h1{
padding: 6% 0;
font-family:raleway;
text-align: center;
}
.csv{
margin: auto;
}
.h2{
margin: 0 !important;
padding: 2% 0 !important;
}
img{
padding: 10% 25%;
}
table {
font-family: 'Raleway', sans-serif;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
border:#ccc 1px solid;
font-size: 16px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
width: 90%;
margin: 7% auto;
}
table td{
padding: 18px;
text-align: center;
border-bottom: 1px solid #e0e0e0;
border-right: 1px solid #e0e0e0;
}thead{
font-weight: bold;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}

Conclusion:

I hope that at this point you must be feeling yourself comfortable with exporting yahoo contacts in PHP. Please comment for any query. Keep visiting our website.