Examples » Grabbing GMail Contact List
You know the social networking sites out there, they have cool feature like 'refer-a-friend', 'invite-friends', or something similar. It works like this: you supply your Yahoo/MSN/GMail username and password, they grab your contact list and display the email addresses, then you choose which friends to get invited. Ever wonder how they do this? Well, you've came to right place.
I will show you how to grab your contacts using phpWebHacks. I will use GMail as an example, but you can grab contacts from the other mail providers once you know how.
First, open GMail.
include 'phpWebHacks.php';
$h = new phpWebHacks;
/* open gmail */
$page = $h->get('http://gmail.com');
Google will make several redirects, and you'll get the last HTML page which is the login page.
Now we want to login. But we also have to include the hidden values in the POST request (take a look at the HTML source). Fortunately, we don't have to manually parse the hidden values since phpWebHacks has a utility for parsing HTML forms, which will return the form's elements in associative array.
"gaia_loginform" is the form's name, refer to the HTML source */
$form = $h->parseForm('gaia_loginform', &$action, $page);
Now fill in your username & password and click the login button.
obtain the fields' name from the HTML source */
$form['Email'] = 'myusername';
$form['Passwd'] = 'mypassword';
/* submit the login form
note that we obtained the form's action URL from parseForm() above */
$page = $h->post($action, $form);
Again, it will make several redirects and returned the last HTML page. It's your inbox page. Go take a look.
Now we want to click the "Contacts" link. If you see the source, "Contacts" is linked to ?v=cl&pnl=a. And that's the URL to fetch.
note that you can use relative URLs, it will be converted
to absolute URL using the last URL as the base URL */
$page = $h->get('?v=cl&pnl=a');
Now you have an HTML page which contains your contacts' email adresses. Extract the email addresses using regular expression below.
preg_match_all('/([a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4}))/si', $page, $m);
/* print them */
echo implode("\n", $m[1]) . "\n";
And logout.
That's it. Now that's easy or what?
By the way, if you turned on the logging functionality, phpWebHacks will log all of the HTTP transaction in .log/headers.txt. It is useful for debugging your HTTP scripts. See the log file for the GMail contacts grabber above.