Php 导入Windows Live联系人

Php 导入Windows Live联系人,php,msdn,windows-live,Php,Msdn,Windows Live,我已经开始从live导入联系人。现在我不知道微软在想什么,但他们确实把他们所做的每件事都复杂化了 对于我的应用程序,获得电话号码非常重要。事实上非常重要,如果你没有电话号码,你的联系就会被跳过。用我的方法我看不到任何电话号码。我想,如果我一个接一个地循环通过每个接触,它会显示出来,但是,唉,没有爱 以下是我的方法: $import_id = time(); $client_id = "xxx"; $redirect_uri = 'redirecturi'; $client_secret = "x

我已经开始从live导入联系人。现在我不知道微软在想什么,但他们确实把他们所做的每件事都复杂化了

对于我的应用程序,获得电话号码非常重要。事实上非常重要,如果你没有电话号码,你的联系就会被跳过。用我的方法我看不到任何电话号码。我想,如果我一个接一个地循环通过每个接触,它会显示出来,但是,唉,没有爱

以下是我的方法:

$import_id = time();
$client_id = "xxx";
$redirect_uri = 'redirecturi';
$client_secret = "xxx";
$code = $_GET['code'];
$grant_type = "authorization_code";

$post = "client_id=$client_id&redirect_uri=$redirect_uri&client_secret=$client_secret&code=$code&grant_type=$grant_type";
$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,false);
$result = curl_exec($curl);
curl_close($curl);
$token = json_decode($result);
$access_token = $token->access_token;
$user_id = $token->user_id;

$url = "https://apis.live.net/v5.0/me/contacts?access_token=$access_token";
$response =  curl_file_get_contents($url);
$response = json_decode($response);
foreach($response->data as $contact) {
    $contact_details = curl_file_get_contents("https://apis.live.net/v5.0/" . $contact->id . "?access_token=$access_token");
    debug($contact_details);
}
die();
然而,我只得到这样的信息(我认识的这个人有一个联系电话,当我在people.live.com上查看他的时候我可以看到):

我的权限请求url(定义范围)如下所示:

https://login.live.com/oauth20_authorize.srf?client_id=clientidkey&scope=wl.basic%20wl.offline_access&response_type=code&redirect_uri=redirecturi
我是否应该添加更多作用域以获取联系人号码?如果是,哪些范围?或者这是不可能的?

从阅读中,电话号码是其中的一部分

要取他们的电话号码,你可以

  • 抓取他们的联系人列表(您已经这样做了)
  • 遍历结果集
  • 从联系人响应中获取用户id。(键
    id
  • 用户
    集合发出请求(使用
    wl.电话号码
    范围)
  • 查看电话号码是否为空
    • 如果它们为空,则跳过迭代
示例phones对象(在
用户
响应中)

所以,


解决方案是使用未记录的作用域
wl。联系人\u电话\u号码
,它有被弃用或锁定的风险,只有Microsoft批准的客户端才能使用它,但在此期间它可以工作

此外,您不需要为每个联系人执行额外的请求,您从
me/contacts
获得的联系人对象已在
phones
对象中包含电话号码

顺便说一下,这是我在测试时使用的代码,我使用了一个避免每次复制/粘贴长且重复的cURL参数,并将请求转换为一行程序

请求许可的代码:

$params = ["client_id" => "...", "scope" => "wl.basic wl.contacts_phone_numbers", "response_type" => "code", "redirect_uri" => "http://sanctuary/contacts_callback.php"];

header("Location: https://login.live.com/oauth20_authorize.srf?".http_build_query($params));
请注意权限请求中额外的
wl.联系人\电话号码
范围

获取访问令牌和检索联系人的代码:

// Composer's autoloader, required to load libraries installed with it
// in this case it's the REST client
require "vendor/autoload.php";

// exchange the temporary token for a reusable access token
$resp = GuzzleHttp\post("https://login.live.com/oauth20_token.srf", ["body" => ["client_id" => "...", "client_secret" => "...", "code" => $_GET["code"], "redirect_uri" => "http://sanctuary/contacts_callback.php", "grant_type" => "authorization_code"]])->json();
$token = $resp["access_token"];

// REST client object that will send the access token by default
// avoids writing the absolute URL and the token each time
$client = new GuzzleHttp\Client(["base_url" => "https://apis.live.net/v5.0/", "defaults" => ["query" => ["access_token" => $token]]]);

// get all the user's contacts
$contacts = $client->get("me/contacts")->json()["data"];

// iterate over contacts
foreach ($contacts as $contact) {
    // if that contact has a phone number object
    if (array_key_exists("phones", $contact)) {
        // iterate over each phone number
        foreach ($contact["phones"] as $phone) {
            // if number isn't blank
            if (!empty($phone)) {
                // do whatever you want with that number
            }
        }
    }
}
以下是带有额外范围的
me/contacts
的外观(减去一些换行符和个人信息):


首先,抛弃cURL,使用一个REST客户端库,比如Guzzle,它将把你所有的cURL代码(身份验证等)转换成一行代码,使你的代码更具可读性。即使这意味着我不能输入电话号码。谢谢你的反馈。我刚刚试过,除了
me/contacts
@AndréDaniel上已有的信息外,它不会返回任何其他信息。你是否添加了范围
wl.电话号码
?@AndréDaniel啊,对不起!今晚下班后我会做一些实验,稍后更新我的答案。今晚我也会玩这个。到目前为止还没有时间。我在这个问题上加了悬赏,马上就破坏了我的网站哈哈。我喜欢编程。我也尝试过这个,但是我得到了@AndréDaniel得到的东西。太棒了!我一整天都在寻找这个答案,谢谢:)
$arrUser = json_decode($strResponse, true);

if( is_null($arrUser['phones']['personal']) 
     AND is_null($arrUser['phones']['business']
      AND is_null($arrUser['phones']['mobile']) ) {
   //No phone numbers
   //Assuming you're in a loop, fetching a user object for each contact - skip the iteration & move onto the next contact.
   continue;
}
$params = ["client_id" => "...", "scope" => "wl.basic wl.contacts_phone_numbers", "response_type" => "code", "redirect_uri" => "http://sanctuary/contacts_callback.php"];

header("Location: https://login.live.com/oauth20_authorize.srf?".http_build_query($params));
// Composer's autoloader, required to load libraries installed with it
// in this case it's the REST client
require "vendor/autoload.php";

// exchange the temporary token for a reusable access token
$resp = GuzzleHttp\post("https://login.live.com/oauth20_token.srf", ["body" => ["client_id" => "...", "client_secret" => "...", "code" => $_GET["code"], "redirect_uri" => "http://sanctuary/contacts_callback.php", "grant_type" => "authorization_code"]])->json();
$token = $resp["access_token"];

// REST client object that will send the access token by default
// avoids writing the absolute URL and the token each time
$client = new GuzzleHttp\Client(["base_url" => "https://apis.live.net/v5.0/", "defaults" => ["query" => ["access_token" => $token]]]);

// get all the user's contacts
$contacts = $client->get("me/contacts")->json()["data"];

// iterate over contacts
foreach ($contacts as $contact) {
    // if that contact has a phone number object
    if (array_key_exists("phones", $contact)) {
        // iterate over each phone number
        foreach ($contact["phones"] as $phone) {
            // if number isn't blank
            if (!empty($phone)) {
                // do whatever you want with that number
            }
        }
    }
}
Array (
    [data] => Array (
            [0] => Array (
                    [id] => contact...
                    [first_name] => ...
                    [last_name] => ...
                    [name] => ...
                    [is_friend] => 
                    [is_favorite] => 
                    [user_id] => 
                    [email_hashes] => ...
                    [updated_time] => ...
                    [phones] => Array ( // what you asked for
                            [personal] => 
                            [business] => 
                            [mobile] => +337...
                        )
                )
        )
)