Hotmail与API联系

时间:2011-03-23 00:08:13

标签: php

是否有任何示例代码使用PHP中的API从Hotmail / Live邮件导入联系人? 感谢。

2 个答案:

答案 0 :(得分:0)

我建议您查看Windows Live Contacts for PHP及其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);

?>   

我自己并没有使用它,但它看起来像你追求的东西。

答案 1 :(得分:0)

是这是完整的代码。 A) - 在“Microsoft帐户开发人员中心”中创建Live.com应用程序,以获取您的客户端ID和客户端密钥。

1) - 创建一个login.php页面并将以下代码粘贴到其中。

<?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) - 现在创建index.php或callback.php //它将被用作重定向页面。

<?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>=> &nbsp;&nbsp;&nbsp;<span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;

?>