Import Yahoo contact to website / download CSV

Image representing OAuth as depicted in CrunchBase

Written in PHP and using cURL, this script imports the names and email addresses from your yahoo account (yahoo id and password needed to login to yahoo from the script and retreive the address book).

<!–?php–>

ob_start();

session_start();

require ‘globals.php’;

require ‘oauth_helper.php’;

require ‘yahoo_function.php’;

// Callback can either be ‘oob’ or a url whose domain must match

// the domain that you entered when registering your application

$callback=’<your call back URL>’;

if($_REQUEST[‘oauth_verifier’] != ”) {

$request_token = $_REQUEST[‘oauth_token’];

$oauth_verifier = $_REQUEST[‘oauth_verifier’];

$request_token_secret = $_SESSION[‘request_token_secret’];

$accessToken = get_access_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,$request_token, $request_token_secret,$oauth_verifier, false, true, true);

$access_token = urldecode($accessToken[3][‘oauth_token’]);

$access_token_secret = urldecode($accessToken[3][‘oauth_token_secret’]);

$guid = $accessToken[3][‘xoauth_yahoo_guid’];

$callcont = callcontact(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $guid, $access_token, $access_token_secret, false, true);

print ‘Total Email COntact :’ .$callcont[‘contacts’][‘total’];

print ‘<br/><br/>’;

print ‘asda’.($callcont[‘contacts’][‘total’]);

for($i=0; $i<=$callcont[‘contacts’][‘total’];$i++) {

print ‘<br/><br/>’;

print $callcont[‘contacts’][‘contact’][$i][‘fields’][0][‘value’];

print ‘<br/><br/>’;

}

}else{

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

if (! empty($retarr)){

$request_token = $retarr[3][‘oauth_token’];

$request_token_secret = $retarr[3][‘oauth_token_secret’];

$request_url = urldecode($retarr[3][‘xoauth_request_auth_url’]);

$_SESSION[‘request_token_secret’] = $request_token_secret;

header(“location:https://api.login.yahoo.com/oauth/v2/request_auth?oauth_token=”.$request_token);

}

exit(0);

}

?>

Yahoo_function.php

<?php

function get_request_token($consumer_key, $consumer_secret, $callback, $usePost=false, $useHmacSha1Sig=true, $passOAuthInHeader=false)

{

$retarr = array(); // return value

$response = array();

$url = ‘https://api.login.yahoo.com/oauth/v2/get_request_token’;

$params[‘oauth_version’] = ’1.0′;

$params[‘oauth_nonce’] = mt_rand();

$params[‘oauth_timestamp’] = time();

$params[‘oauth_consumer_key’] = $consumer_key;

$params[‘oauth_callback’] = $callback;

// compute signature and add it to the params list

if ($useHmacSha1Sig) {

$params[‘oauth_signature_method’] = ‘HMAC-SHA1‘;

$params[‘oauth_signature’] =

oauth_compute_hmac_sig($usePost? ‘POST‘ : ‘GET’, $url, $params,

$consumer_secret, null);

} else {

$params[‘oauth_signature_method’] = ‘PLAINTEXT‘;

$params[‘oauth_signature’] =

oauth_compute_plaintext_sig($consumer_secret, null);

}

// Pass OAuth credentials in a separate header or in the query string

if ($passOAuthInHeader) {

$query_parameter_string = oauth_http_build_query($params, FALSE);

$header = build_oauth_header($params, “yahooapis.com”);

$headers[] = $header;

} else {

$query_parameter_string = oauth_http_build_query($params);

}

// POST or GET the request

if ($usePost) {

$request_url = $url;

logit(“getreqtok:INFO:request_url:$request_url”);

logit(“getreqtok:INFO:post_body:$query_parameter_string”);

$headers[] = ‘Content-Type: application/x-www-form-urlencoded‘;

$response = do_post($request_url, $query_parameter_string, 443, $headers);

} else {

$request_url = $url . ($query_parameter_string ?

(‘?’ . $query_parameter_string) : ” );

logit(“getreqtok:INFO:request_url:$request_url”);

$response = do_get($request_url, 443, $headers);

}

// extract successful response

if (! empty($response)) {

list($info, $header, $body) = $response;

$body_parsed = oauth_parse_str($body);

if (! empty($body_parsed)) {

logit(“getreqtok:INFO:response_body_parsed:”);

}

$retarr = $response;

$retarr[] = $body_parsed;

}

return $retarr;

}

function get_access_token($consumer_key, $consumer_secret, $request_token, $request_token_secret, $oauth_verifier, $usePost=false, $useHmacSha1Sig=true, $passOAuthInHeader=true)

{

$retarr = array(); // return value

$response = array();

$url = ‘https://api.login.yahoo.com/oauth/v2/get_token’;

$params[‘oauth_version’] = ’1.0′;

$params[‘oauth_nonce’] = mt_rand();

$params[‘oauth_timestamp’] = time();

$params[‘oauth_consumer_key’] = $consumer_key;

$params[‘oauth_token’]= $request_token;

$params[‘oauth_verifier’] = $oauth_verifier;

// compute signature and add it to the params list

if ($useHmacSha1Sig) {

$params[‘oauth_signature_method’] = ‘HMAC-SHA1′;

$params[‘oauth_signature’] =

oauth_compute_hmac_sig($usePost? ‘POST’ : ‘GET’, $url, $params,

$consumer_secret, $request_token_secret);

} else {

$params[‘oauth_signature_method’] = ‘PLAINTEXT’;

$params[‘oauth_signature’] =

oauth_compute_plaintext_sig($consumer_secret, $request_token_secret);

}

// Pass OAuth credentials in a separate header or in the query string

if ($passOAuthInHeader) {

$query_parameter_string = oauth_http_build_query($params, false);

$header = build_oauth_header($params, “yahooapis.com”);

$headers[] = $header;

} else {

$query_parameter_string = oauth_http_build_query($params);

}

// POST or GET the request

if ($usePost) {

$request_url = $url;

logit(“getacctok:INFO:request_url:$request_url”);

logit(“getacctok:INFO:post_body:$query_parameter_string”);

$headers[] = ‘Content-Type: application/x-www-form-urlencoded’;

$response = do_post($request_url, $query_parameter_string, 443, $headers);

} else {

$request_url = $url . ($query_parameter_string ?

(‘?’ . $query_parameter_string) : ” );

logit(“getacctok:INFO:request_url:$request_url”);

$response = do_get($request_url, 443, $headers);

}

// extract successful response

if (! empty($response)) {

list($info, $header, $body) = $response;

$body_parsed = oauth_parse_str($body);

if (! empty($body_parsed)) {

logit(“getacctok:INFO:response_body_parsed:”);

//print_r($body_parsed);

}

$retarr = $response;

$retarr[] = $body_parsed;

}

return $retarr;

}

function callcontact($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost=false, $passOAuthInHeader=true)

{

$retarr = array(); // return value

$response = array();

$url = ‘http://social.yahooapis.com/v1/user/’ . $guid . ‘/contacts’;

$params[‘format’] = ‘json’;

$params[‘view’] = ‘compact’;

$params[‘oauth_version’] = ’1.0′;

$params[‘oauth_nonce’] = mt_rand();

$params[‘oauth_timestamp’] = time();

$params[‘oauth_consumer_key’] = $consumer_key;

$params[‘oauth_token’] = $access_token;

// compute hmac-sha1 signature and add it to the params list

$params[‘oauth_signature_method’] = ‘HMAC-SHA1′;

$params[‘oauth_signature’] =

oauth_compute_hmac_sig($usePost? ‘POST’ : ‘GET’, $url, $params,

$consumer_secret, $access_token_secret);

// Pass OAuth credentials in a separate header or in the query string

if ($passOAuthInHeader) {

$query_parameter_string = oauth_http_build_query($params, true);

$header = build_oauth_header($params, “yahooapis.com”);

$headers[] = $header;

} else {

$query_parameter_string = oauth_http_build_query($params);

}

// POST or GET the request

if ($usePost) {

$request_url = $url;

logit(“callcontact:INFO:request_url:$request_url”);

logit(“callcontact:INFO:post_body:$query_parameter_string”);

$headers[] = ‘Content-Type: application/x-www-form-urlencoded’;

$response = do_post($request_url, $query_parameter_string, 80, $headers);

} else {

$request_url = $url . ($query_parameter_string ?

(‘?’ . $query_parameter_string) : ” );

logit(“callcontact:INFO:request_url:$request_url”);

$response = do_get($request_url, 80, $headers);

}

// extract successful response

if (! empty($response)) {

list($info, $header, $body) = $response;

if ($body) {

logit(“callcontact:INFO:response:”);

json_pretty_print($body);

}

$retarr = $response;

}

$contactsRes = json_decode($retarr[2],true);

return $contactsRes;

}

?>

  1. No trackbacks yet.

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

%d bloggers like this: