0

I'm receiving all of my contacts on Google Contacts using this method:

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>';

}

It works fine but it's not sorting the contacts alphabetically. How can I do this?

Thanks in advance.

Airikr
  • 6,258
  • 15
  • 59
  • 110

2 Answers2

1

I assume that you have an array in $response_as_array['feed']['entry'] which have all your contacts, then you can use:

usort($response_as_array['feed']['entry'], function($a, $b) { 
    return strcmp($a['gd$name']['gd$fullName']['$t'], $b['gd$name']['gd$fullName']['$t']);
});
m4t1t0
  • 5,669
  • 3
  • 22
  • 30
  • Thanks but I don't know how exactly I'm going to use that. The code I'm showing in my question is the full code for the function I currently using. How do put this code with mine so it sorts alphabetically? – Airikr Jan 07 '13 at 18:51
  • I have edited my answer, but you need to check if the code is valid. I don't know the array structure and perhaps, you can use other values to do the comparison better. – m4t1t0 Jan 07 '13 at 19:22
  • I have one problem with your solution though. I'm using PHP 5.3 on my WAMP but my web host are using PHP 5.2.17. How can I use your solution in this version of PHP? – Airikr Jan 07 '13 at 21:32
  • With PHP 5.2 you can not use anonymous functions, you need to declare the function with a name and then call your function in usort. Check the usort manual for more info. – m4t1t0 Jan 07 '13 at 21:44
0

You could try asort(): http://php.net/manual/en/function.asort.php

You may have to re-arrange the data from Google to do this temporarily.

To be honest I would have imagined Google to provide some additional conditions you could use to get them to sort it for you. Which would be appended to the request URL.

diggersworld
  • 12,770
  • 24
  • 84
  • 119