使用cURL按字母排序我的联系人


Sort my contacts alphabetic using cURL

我正在使用这个方法接收Google联系人中的所有联系人:

session_start();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ClientLogin');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = array(
    'accountType' => 'GOOGLE',
    'Email' => 'email',
    'Passwd' => 'password',
    'source' => 'sourcetest',
    'service' => 'cp'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$responses = explode("'n", curl_exec($ch));
$_SESSION['auth'] = str_replace('Auth=', '', $responses[2]);
$_SESSION['email'] = 'email';

$url = 'https://www.google.com/m8/feeds/contacts/default/full';
$url .= '?group=http://www.google.com/m8/feeds/groups/'.$_SESSION['email'].'/base/6';
$url .= '&max-results=500&alt=json';
$ch = curl_init($url);
$header[] = 'Authorization: GoogleLogin auth='.$_SESSION['auth'];
$header[] = 'GData-Version: 3.0';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = curl_exec($ch);
curl_close($ch);

/** **** **** **** **** **** **** **** **** **** **** **/

# CONTROL
if(isset($response_as_array['feed']) AND isset($response_as_array['feed']['entry'])) {
    # TABLE
    echo '<table width="100%" cellpadding="0" cellspacing="0">';
    echo '<tr>';
        echo '<td align="left" class="padding-td" width="180">';
            echo '<b>Namn</b>';
        echo '</td>';
        echo '<td align="left" class="padding-td">';
            echo '<b>Telefon-nummer</b>';
        echo '</td>';
    echo '</tr>';

    # LOOP
    foreach($response_as_array['feed']['entry'] AS $i => $entry) {
        # CONTROL: Hide my name
        if(utf8_decode($entry['gd$name']['gd$fullName']['$t']) != 'Erik Edgren') {
            echo '<tr>';
                echo '<td align="left" class="padding-td">';
                    echo utf8_decode($entry['gd$name']['gd$fullName']['$t']);
                echo '</td>';
                echo '<td align="left" class="padding-td">';
                    echo isset($entry['gd$phoneNumber'][0]) ? $entry['gd$phoneNumber'][0]['$t'] : '';
                echo '</td>';
            echo '</tr>';
        }
    }

    echo '</table>';
}

它工作得很好,但它不是按字母顺序排序的联系人。我该怎么做呢?

我假设您在$response_as_array['feed']['entry']中有一个数组,其中包含您的所有联系人,那么您可以使用:

usort($response_as_array['feed']['entry'], function($a, $b) { 
    return strcmp($a['gd$name']['gd$fullName']['$t'], $b['gd$name']['gd$fullName']['$t']);
});

您可以尝试asort(): http://php.net/manual/en/function.asort.php

你可能需要重新排列来自Google的数据来暂时做到这一点。

老实说,我会想象谷歌提供一些额外的条件,你可以使用,让他们为你排序。