无法使用php从Microsoft Outlook访问数据

时间:2018-05-03 09:35:03

标签: php outlook contacts

我已经设置了一个Microsoft Outlook帐户用于测试目的。我已经输入了几个联系人并向该帐户发送了电子邮件,因此它显然是活跃的。我已经编写了PHP代码来访问该数据,但没有返回任何内容。其他一切似乎都在起作用,访问令牌和验证似乎都没问题。

php代码如下(' xxxx'显然已被实际代码替换)用于诊断目的的回声,为清楚起见,有些行已被破坏。

<?php
//***************************************MSN 
START********************************
$client_id = 'xxxxxx';
$client_secret = 'xxxxx';
$redirect_uri =         
'http://localhost:8000/testcontacts/oauth_hotmail.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;
$msn_link =  '<a href="'.$urls_.'" >MSN Contacts</a>';
echo $msn_link;
//***************************************MSN 
ENDS********************************
?>

<?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 = 'xxxxx';
$client_secret = 'xxxxx';
$redirect_uri = http://localhost:8000/testcontacts/oauth_hotmail.php';
$auth_code = $_GET["code"];

echo '<br>' . $auth_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;

echo '<br>' . $accesstoken;

$url = 'https://apis.live.net/v5.0/me/contacts? 
access_token='.$accesstoken.'&limit=100';
$xmlresponse =  curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);

echo '<br>' . $url;
echo '<br>' . $xmlresponse;
echo '<br>' . $xml;

$msn_email = "";
foreach($xml['data'] as $emails)
{
// echo $emails['name'];
$email_ids = implode(",",array_unique($emails['emails'])); //will get 
more email primary,sec etc with comma separate
$msn_email .= "<div><span>".$emails['name']."</span> 
&nbsp;&nbsp;&nbsp;<span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;

?>

我收到以下回复(...代表长字符串): -

MSN Contacts 
Md9a1ad3b-d405-5f3f-1b08-10ae540b5669
EwAgA61DBAAUcSSzoTJJs.....
https://apis.live.net/v5.0/me/contacts?access_token=EwAgA61DBAAUcSSzo.....=&limit=100
{ "data": [], "paging": { } }
Array

请有人告诉我为什么没有退回。谢谢。

1 个答案:

答案 0 :(得分:0)

如果有其他人这样,我现在已经开始工作了。我在互联网上看到的所有代码都比我想要的或者完全拒绝工作要复杂得多,所以对我来说没用多少。在这里,我设法将代码减少到一个独立的工作代码中。我希望它适合你。

还有一个问题。我看不到如何获取特定联系人列表中的联系人,也无法获取所有联系人。有什么想法吗?

这是代码......

<?php
//***************************************MSN START********************************
$client_id = 'xxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxx';
$redirect_uri = 'https://.........php';
$login_uri = 'https://login.live.com/oauth20_authorize.srf';
$scope = '&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails';
$urls_ = $login_uri;
$urls_ .= '?client_id='.$client_id;
$urls_ .= $scope.'&response_type=code';
$urls_ .= '&redirect_uri='.$redirect_uri;

$msn_link =  '<a href="'.$urls_.'" >Get Contacts</a>';
echo $msn_link;
//***************************************MSN ENDS********************************

if(isset($_GET["code"])){
    $auth_code     = $_GET["code"];

    // get contacts
    $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;

    $apiurl = "https://outlook.office.com/api/v2.0";
    $headers = array(
        "User-Agent: php-tutorial/1.0",
        "Authorization: Bearer ".$accesstoken,
        "Accept: application/json",
        "return-client-request-id: true"
        );

    $search = array (
    // Only return selected fields
        "\$select" => "EmailAddresses,GivenName,Surname",
    // Sort by GivenName
        "\$orderby" => "GivenName ASC"
        );

    $outlookApiUrl = $apiurl . "/me/contacts?" . http_build_query($search);
    $response = runCurl($outlookApiUrl, null, $headers);
    $response = explode("\n", trim($response));
    $response = $response[count($response) - 1];
    $response = json_decode($response, true);

    foreach ($response["value"] as $contacts) {
        $email = $contacts["EmailAddresses"][0]["Address"];
        echo '<br>' . $contacts["GivenName"] . ' ' . $contacts["Surname"] . ' ' . $email;
        }
    }  

function runCurl($url, $post = null, $headers = null) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, $post == null ? 0 : 1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if($http_code >= 400) {
        echo "Error executing request to Office365 api with error code=$http_code<br/><br/>\n\n";
        echo "<pre>"; print_r($response); echo "</pre>";
        die();
        }
    return $response;
    }
?>