【问题标题】:Sort my contacts alphabetic using cURL使用 cURL 对我的联系人进行字母排序
【发布时间】:2013-01-07 18:32:39
【问题描述】:

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

}

它工作正常,但它没有按字母顺序对联系人进行排序。我该怎么做?

提前致谢。

【问题讨论】:

    标签: php sorting curl


    【解决方案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']);
    });
    

    【讨论】:

    • 谢谢,但我不知道我将如何使用它。我在问题中显示的代码是我当前使用的函数的完整代码。如何将此代码与我的代码一起按字母顺序排序?
    • 我已经编辑了我的答案,但您需要检查代码是否有效。我不知道数组结构,也许您可​​以使用其他值来更好地进行比较。
    • 我对你的解决方案有一个问题。我在我的 WAMP 上使用 PHP 5.3,但我的网络主机使用的是 PHP 5.2.17。我如何在这个版本的 PHP 中使用你的解决方案?
    • 在 PHP 5.2 中你不能使用匿名函数,你需要用名字声明函数,然后在 usort 中调用你的函数。查看 usort 手册了解更多信息。
    【解决方案2】:

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

    您可能需要临时重新整理来自 Google 的数据。

    老实说,我本以为 Google 会提供一些额外的条件,您可以使用它们来让他们为您排序。这将附加到请求 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多