Hotmail contacts with API
Is there any example code of importing contacts from Ho开发者_开发知识库tmail/Live mail using their API in PHP? Thanks.
I suggest you taking a look at Windows Live Contacts for PHP and their simplest example:
<?php
// COPIED FROM http://livecontactsphp.codeplex.com/SourceControl/changeset/view/6336#150263
// Example of how to use the library -- contents put in $ret_array
include "contacts_fn.php";
$ret_array = get_people_array();
//to see a array dump...
var_dump($ret_array);
?>
I haven't used that myself, but it looks like thing you are after.
Yes Here is the complete code. A)- Create a Live.com application in “Microsoft account Developer Center” for obtaining your Client ID and Client Secret.
1)- create a login.php page and paste the following code in it.
<?php
session_start();
$client_id = 'YOUR CLIENT ID';
$client_secret = 'YOUR CLIENT SECRETE';
$redirect_uri = 'http://example.com/callback.php';
$urls_ = 'https://login.live.com /oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails&response_type=code&redirect_uri='.$redirect_uri;
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Import Hotmail Contacts</title>
</head>
<body>
<?php echo '<a href="'.$urls_.'" ">Contact From MSN/Hotmail/outlook</a>';?>
</body>
</html>
2) - Now create index.php or callback.php //it will be used as redirected page.
<?php
//function for parsing the curl request
function curl_file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$client_id = 'YOUR CLIENT ID';
$client_secret = 'YOUR CLIENT SECRET';
$redirect_uri = 'http://example.com/callback.php';
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=2';
$xmlresponse = curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);
$msn_email = "";
foreach($xml['data'] as $emails)
{
// echo $emails['name'];
$email_ids = implode(",",array_unique($emails['emails']));
$msn_email .= "<div><span>".$emails['name']."</span>=> <span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;
?>
精彩评论