简体   繁体   中英

Unable to access data from Microsoft Outlook with php

I have set up a Microsoft Outlook account for testing purposes. I have entered a couple of contacts and sent emails to/from that account, so it is clearly active. I have written php code to access that data, but nothing is returned. Everything else seems to be working in that the access tokens and verification all seems OK.

The php code is as follows (the 'xxxx' is obviously replaced with an actual code) with echos for diagnostic purposes, and some lines have been broken for clarity here.

<?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;

?>

I get the following response (... represents long character string):-

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

Please can anyone tell me why nothing is returned. Thank you.

If anyone else comes this way, I have now got this to work. All the code that I have seen on the internet has been much more complicated than I wanted or refused to work at all, so not much use to me. Here I have managed to pare the code down to a minimum in a stand-alone working piece of code. I hope it works for you.

One question still remains. I cannot see how to get the contacts which are in a specific contact list, and not get all the contacts. Any ideas?

Here is the code...

<?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;
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM