Hey, Folks!

In this blog, we’re going to learn a smarter way of managing email list. We will tell you how to import a contact in user’s yahoo address book from an external application.

We’re going to use Yahoo’s Contact API to achieve this goal. You can take a reference from our previous blog that how we can log in and gain access through Yahoo Contact API to read/write contacts.

You can also export contacts from yahoo in a CSV file with the same procedure. Take a sneak peek at our blog Export Yahoo Contacts.
Apart from Contact API, Yahoo’s YOS library will also get used.
So, let’s take a look at the process.



For Complete solution, you can take a look at our premium service MailGet – email marketing platform.


Files to be needed:

We’re going to use YOS Library which you can get from the github repository.


Process:

Let’s take a look at the process step by step.

  1. The user will get a contact form on the index page.
  2. The user will be asked to enter his First Name, Last Name, and Email Address.
  3. After submitting the above information, the user will be asked to log into yahoo account in which he wants to add the contact.
  4. Then, yahoo will ask the user for permission to read/write contacts from/to his account.
  5. And at last, contact will get added in his address book.
  6. After this, he can get back to the first page with the back button, or he can log out from logout button.

So, above is the complete process to add contacts in yahoo.


PHP File: index.php

<html>
<head>
<title>Insert Contacts in Yahoo Using PHP</title>
<!--Files to be included -->
<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>Insert Contacts in Yahoo Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="col-md-12"><h2><span>Insert Contact in <img id="logo" src="images/yahoo.png"/></span></h2></div>
</div>
<div class="row">
<div class="col-md-12">
<form action="http://formget.com/tutorial/insert-contacts-in-yahoo-using-php/contact-add.php?<?php echo http_build_query($data); ?>" method="get">
<input type="text" placeholder="First Name" name="name"/>
<input type="text" placeholder="Last Name" name="lname"/>
<input type="email" placeholder="Contact Email" name="email"/>
<input type="submit" value="Add Contact" name="submit"/>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
<?php
if(isset($_GET['submit']))
{
$data['name'] = $_GET['name'];
$data['lname'] = $_GET['lname'];
$data['email'] = $_GET['email'];
}
?>

PHP File: contact-add.php

<html>
<head>
<title>Insert Contacts in Yahoo 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>Insert Contacts in Yahoo Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="col-md-2"><a href="http://formget.com/tutorial/insert-contacts-in-yahoo-using-php/index.php"><img id="yprevious" src="images/previous.png"/></a></div>
<div class="col-md-8"><h2><span>Insert Contact in <a><img src="images/yahoo.png"/></a></span></h2></div>
<div class="col-md-2"><a href="#" onclick="caller()"><img id="ylogout" src="images/logout.png"/></a></div>
</div>
<div class="row">
<div class="col-md-12">
<p>Contact Added</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p id="note">Note : If you want to export your yahoo contacts, then you can use <a href="https://www.formget.com/tutorial/export-yahoo-contacts-using-php/">Export Yahoo Contacts</a></p>
</div>
</div>
</div>
</body>
</html>

<?php
require("Yahoo.inc");
session_start();
// Your Consumer Key (API Key) goes here.
define('CONSUMER_KEY', "INSERT YOUR APPLICATION CONSUMER KEY");

// Your Consumer Secret goes here.
define('CONSUMER_SECRET', "INSERT YOUR APPLICATION CONSUMER SECRET");

// Your application ID goes here.
define('APPID', "INSERT YOUR APPLICATION ID");
$session = YahooSession::requireSession(CONSUMER_KEY,CONSUMER_SECRET,APPID);

// Fetch the logged-in (sessioned) user
$user = $session->getSessionedUser();
if(isset($_GET)){
$data = $_GET;
$name = $data['name'];
$lname = $data['lname'];
$email = $data['email'];
$contact_fields = array();
$contact_fields[] = array('type' => 'email', 'value' => $email);
$contact_fields[] = array('type' => 'name', 'value' => array('givenName'=> $name, 'familyName' => $lname));
$contact = array('fields' => $contact_fields);
print_r($user->addContact($contact)) ;
}
?>

CSS File: style.css

@import url(http://fonts.googleapis.com/css?family=Raleway);
body{
font-family:raleway !important;
}
#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 !important;
}
.wrap{
width: 33%;
margin: 5% auto;
}
.container-fluid{
width: 45%;
margin: auto auto;
}
#login{
border: 2px solid #ccc;
border-radius: 10px;
font-family:raleway!important;
}
h1{
padding: 6% 0;
font-family:raleway!important;
text-align: center;
}
.h2{
margin: 0 !important;
padding: 2% 0 !important;
}
img{
padding: 0% 0%;
width: 110px;
margin-bottom: -5%;
}
input[type=text],input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 14px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway !important;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-top: 14px;
}
p{
text-align: center;
font-family:raleway !important;
font-size:25px;
padding: 15px;
}
#yprevious,#ylogout{
width: 50px;
margin-top: 25%;
}
#logo {
margin-bottom: -2%;
}
#note{
font-size: 20px;
}
#note a{
text-decoration: none;
}

JavaScript 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 = 'http://formget.com/tutorial/insert-contacts-in-yahoo-using-php/index.php';
}

Explaination of the main file : Contact-add.php

First of all we’ll include Yahoo.inc file

require("Yahoo.inc");

Then we will start the session

session_start();

We need to define the Consumer Key, we had generated while creating the application.

define('CONSUMER_KEY', "INSERT YOUR APPLICATION CONSUMER KEY");

We need to define the Consumer Secret.

define('CONSUMER_SECRET', "INSERT YOUR APPLICATION CONSUMER SECRET");

You may or may not insert application ID.

define('APPID', "INSERT YOUR APPLICATION ID");

Now we’ll store the session created for the particular application.

$session = YahooSession::requireSession(CONSUMER_KEY,CONSUMER_SECRET,APPID);

Below line will fetch the logged-in (sessioned) user

$user = $session->getSessionedUser();

Below code is simply catching array thrown from index page.

if(isset($_GET)){
$data = $_GET;
 $name = $data['name'];
$lname = $data['lname'];
$email = $data['email'];

Below code is use to prepare contact in the proper format which is to be inserted.

$contact_fields = array();
$contact_fields[] = array('type' => 'email', 'value' => $email);
$contact_fields[] = array('type' => 'name', 'value' => array('givenName'=> $name, 'familyName' => $lname));
$contact = array('fields' => $contact_fields);

Below code will insert the contact in user’s address book of yahoo.

$user->addContact($contact);

Conclusion:

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

Get more related blogs here –