0

我正在使用此方法接收我在 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>';

}

它工作正常,但它没有按字母顺序对联系人进行排序。我怎样才能做到这一点?

提前致谢。

4

2 回答 2

1

我假设您有一个$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']);
});
于 2013-01-07T18:43:59.740 回答
0

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

您可能需要重新安排来自 Google 的数据才能临时执行此操作。

老实说,我会想象谷歌会提供一些额外的条件,你可以使用它们来让他们为你排序。它将附加到请求 URL。

于 2013-01-07T18:37:49.487 回答