使用cURL按字母顺序对联系人进行排序

时间:2013-01-07 18:32:39

标签: php sorting 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>';

}

它工作正常,但它没有按字母顺序排序联系人。我怎么能这样做?

提前致谢。

2 个答案:

答案 0 :(得分: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']);
});

答案 1 :(得分:0)

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

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

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