我正在尝试从Windows Live Contacts API获取我的hotmail联系人。我按照教程下载了php和html代码。即使它应该工作并且它给了我一把钥匙,它也不会显示任何东西。
当我尝试获取联系人时,它首先会征求我的许可,然后我会自动返回我自己的页面,这就是我收到错误的时候。
错误:未找到
请求的网址 /result.php?code=xxxxxx-xxx-xx 在此服务器上。
索引.php
<?php
include('config.php');
$url = '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;
?>
<html>
<body link="#0C6182" vlink="#0C6182" alink="#0C6182">
<a href="<?php print $url; ?>" style="text-decoration: none"> Invite Friends From Hotmail</a>
</body>
</html>
结果.php
<?php
session_start();
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;
}
?>
<html>
<body >
<div align="center">
<table>
<tr>
<td>
<?php
//setting parameters
include('config.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;
$xmlresponse = curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);
$contacts_email = "";
$count = 0;
foreach ($xml['data'] as $title) {
$count++;
echo $count.". ".$title['emails']['personal'] . "<br><br>";
}
?>
</td>
</tr>
</table>
</div>
</body>
</html>
配置.php
<?php
$client_id = 'xxxxxxxx';
$client_secret = 'xxxxxx-xxxxxxxxxx';
$redirect_uri = 'xxxxxxxxx';
?>
在用户同意后,WindowsLive 无法正确重定向到您的$redirect_uri
:请验证值是否正确。