Category: PHP

Gmail Contact Importer

Import your Gmail Contacts the fast and easy way (insert Billy Mays voice here)

My overall objective was to build very simple API that would allow a user to download his/her Gmail contacts and display them in a specific format.

In my opinion a API like this should be very popular amongst the social networking sites.

The API has the capability to be configured to show results in several different formats XML, JSON, and PHP format. The PHP format returns the results in an array format.

The operating format is as follows.

  • Source:contacts
  • Gmail:Username
  • Gmail:Password
  • Format:PHP, or XML, or JSON

http://apime.josephtinsley.com/contacts/{user_name}:{user_password}/{format}

http://apime.josephtinsley.com/contacts/bob:I_luv_Tacos/json

A simple way of including the result set into your PHP web page is to use PHP’s file_get_contents function.

1
2
3
4
5
6
7
8
<?PHP

$results = file_get_contents('http://apime.josephtinsley.com/contacts/{Email_Address}:{Password}/{Format}');
print '<pre>';
print_r($results);
print '<pre>';

?>

*results are limited to 25 contacts right now*
Thanks,
Joe

Google’s Analytics API with PHP

I was recently asked about my experience with Google’s Analytics API, my reply was bla, bla, bla, I do not have any experience with their API’s.  But to be honest I couldn’t remember why at that moment I didn’t. I think the only reason was because of their documentation, for some of their API’s it’s a pretty long, difficult read. But overall that wasn’t a very good reason, so I decided to take some time and dive in and whoop that trick (line from the hustle and flow movie).
The goal for this post to help anyone who may be having a hard time understanding how to build a connection to Google’s Analytics API.
So first there are a couple of things you need to know.

There are only two files needed in this script but the connection part is the only part we will be going over, it’s in the gaClass.php file.
So basically I’m still new to  Google’s Analytics API but what I’ve learned so far is that once you’ve established a connection the rest is pretty simple.  So basically that’s why we will be focusing on the establishing a connection part only.

Your first major goal while trying to establish a connection is to collect the authentication string. Within that string is your authentication token, and without that token you can’t get anywhere so remember that. The overall objective is to get the authentication token.

So open up the gaClass.php file and take a look at the construct method, this is where the connection happens. Please contact me if I confuse your somewhere, I try to code so that it’s easy to follow.

  • Within the construct method we start off by collection your email and password that was set on the form page. They of course are needed to connect to your account.
  • We set the url that we will be trying to making a connection to. In this case it’s the ClientLogin URL (https://www.google.com/accounts/ClientLogin) . Take note that the protocol starts with HTTPS.
  • Within our POST request we have a few additional parameters that will need to be pass along including your email and password. A short description of these parameters can be found at (http://code.google.com/apis/analytics/docs/gdata/gdataAuthentication.html) scroll down until you see the HTTP Protocol Details heading
  • Now once we have all the parameters set we will try to actually make the connection. We do this within the next method. The curlConnection method, and it does exactly what it says, tries to makes the connection via cURL
  • The curlConnection method will take the parameters we talked about earlier and send them to the ClientLogin URL via cURL using a POST method. The answer we receive from this post will or will not include our authentication token.
  • Now let’s suppose it does and the header status code equal 200, which means it’s A-OK. Within that connection response output you will see a encrypted string. But look closers you’ll see three variables with three values assigned to it. The variables are labeled , “SID”, “LSID” and  “AUTH”. Well you want the AUTH and it value.  Our next task is to parse out the AUTH variable and its value, and we do this with the setAccessToken method.
  • The setAccessToken is fairly simple we take that string and convert it to an array using PHP’s built in explode function.  The access token should be in the second element. I then assign this value to a token variable to be used later for accessing other parts of the API. If you recall when we started this post the authentication token is essential to other parts of the API. Without it you basically can’t do anything.

So that you can get a idea of what the output data look like.  I’ve included a screen shot of the header response output, the authentication output, when the authentication output is converted into a array, and the authentication token.

Googles Analytics API Header Response

 

 

 

 

 

 

 

 

 

 

 

 

I’m new to Mercurial so please don’t give me a hard time about my commits or the lack of.
https://bitbucket.org/josephtinsley/google-analytics-api/overview

Google Analytics API with PHP

 

 

My First Mongo PHP APP

Today I created my first MongoDB application. Please no applause, this app is incredibly boring.  At the time I was fresh out of cool ideas, especially ideas that required a nice amount of data.
So I went with a simple US Population search script. I actually had a copy of the 2009 census data in a mysql table from some time ago. I have no idea why, I guess I’m a hoarder like that.
But basically I built this just for the experience. I’ve been hearing a lot of hub bub about MongoDB and thought I should give it a try.  I have to say from what I’ve fiddled with so far it is pretty interesting. If you’re coming from a RDBMS like I was, it’s going to take some time to change your thought process.

MongoDB store its data in what they call documents which are somewhat like table rows. I’m not going to go down that road because I’m still a newbie myself and I don’t want to put anyone on the wrong path. But what helped me get along was that I envisioned accessing data from a XML feed. Of course MongoDB documents are not XML feeds but they are structured a bit like a feed.

For example goto Buy.com and view the source of one of their product feeds (Example Product Feed) look specifically at each product node. When accessing data from MongoDB document I put myself in the mindset as if I’m grabbing data from a XML product feed.  Crazy, but it works for me.

Mongo Document Mongovue

Bottom line I think MongoDB is pretty interesting, I’m still new to it so I’m not quite ready to ditch MySql.
During my quest for knowledge I found myself looking for examples to help me get started. In my opinion there wasn’t that much out there but that’s probably because it’s fairly still new. But here are few sites that help me get started.

Reverse IP Look up .V2

I’ve updated my Reverse IP Look up script. This script/tool will attempt to display a list of domain names that are hosted on the same IP address.

My original script was a bit slow, this one should display results a lot  faster. I’ve also add a simple API that can be configure to show results in different formats XML, JSON, and PHP format. The PHP format returns the results in an array format.

The GUI interface is located in the SEO tools section of this site Reverse IP Look up

To access the API, use the URL below.

Just include your domain name where the curly brackets are, then pick a format to display the results in.

Example

http://apime.josephtinsley.com/{domain_name}/php/
http://apime.josephtinsley.com/bk.com/php/

Thanks,

Joseph

== UPDATED APRIL 06, 2011 ==
In the near future I plan to add more API’s
To handle this change you are now required to add the API’s source that you want to use.
The Reverse IP Look Up source is “reverse

So or example
Instead of
http://apime.josephtinsley.com/{domain_name}/php/
It will need to be
http://apime.josephtinsley.com{source}/{domain_name}/php/
so
http://apime.josephtinsley.com/reverse/bk.com/php/
would be the finished URL.

Thanks again,
Joe

Make Twitter Updates with PHP and cURL

I’m sure I’ve mentioned sometime before that I’m a big fan of Twitter. I think Twitter has done a great job with their API, and the documentation for their API. They’ve made it fairly simple for developers to create Twitter based applications without jumping through a lot of hoops.

Well below is a bit of code I’ve used in the past to make status updates to Twitter. If your familiar with cURL then you’ll see that its pretty straight forward. The end result examines the HTTP code to determine if the status update was successful or failed you can comment this part out if you wish.

But before you go plowing away with the code I think it would be a good idea if you make yourself familiar with their documentation. They have a few rules that could save you some heartache down the road.

== Twitter Documentation ==
http://apiwiki.twitter.com/Twitter-API-Documentation

== PHP Code To Post Twitter Status Updates ==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?PHP

/* Post Twitter Status Update using PHP & cURL */

function postToTwitter($username,$password,$message){
 
   $username = $username;
   $password = $password;
   $twitterHost = "http://twitter.com/statuses/update.xml";
   $yourStatus = $message;
   $curl;
   
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
   curl_setopt($curl, CURLOPT_HEADER, false);
   curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
   curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
   curl_setopt($curl, CURLOPT_POSTFIELDS, "status=". urlencode(stripslashes(urldecode($yourStatus))));
   curl_setopt($curl, CURLOPT_URL, $twitterHost);
   
   $result = curl_exec($curl);
   $resultArray = curl_getinfo($curl);
   
   if ($resultArray['http_code'] == 200) {
   
   $twitterPostStatus = "Success";
   
   } else {
   
   $twitterPostStatus = "Failed";
   
   }
   curl_close($curl);
   
 return $twitterPostStatus;  
}

$username = 'username_here';
$password = 'password_here';
$message = "Working with the Twitter API";

$result = postToTwitter($username,$password,$message);

print_r($result);
?>

Fixed Broken Links

I just noticed I had some broken links on my PHP Scripts page. I repaired the links so the downloads should be back to normal.
I have to say I looked over the code that was used to make this scripts and a lot of it is old. I’m leaving it in place hoping that it can still in some way be helpful.
Later,
Joe

– – UPDATED —
I included two scripts that I had on the old site that somehow was forgotten when I made the move.

Geolocation by IP Address with PHP

Something kind of interesting happened today. I was going through my log files and noticed a query for “joe Tinsley woodland park” the query originated from “Medford, Massachusetts”. Woodland Park was an elementary school located on an air force base in South Carolina that I attended when I was a kid. I take it that someone from my past is trying to locate me. That’s pretty cool. The crazy part is I just noticed that I don’t have anyway for anyone to contact me via my website… Ha! I make the craziest moves sometimes.
Well I’ve mentioned all of this to say If you’re looking for a way to pull Geolocation Data from a single IP address try IPpages.com’s APIs. I’ve been using them for some time now and the service is pretty good. It does require some programming knowledge to the extract the data from the XML results.
Please let me know if I can help with any programming questions (PHP only!).

Google XML Sitemap: Export Title, Description and Keywords to Excel

Google XML Sitemap: Download the Title, Description & Keywords tags from your sites webpage via your Google XML sitemap and then export the data into a excel spreadsheet. I’m not totally sure what to call this tool yet. I started out creating something else and took a totally different direction. That’s not to say this is not a useful tool. Basically if you’ve ever been in a situation and wonder how many URL’s are included in a Google XML sitemap, then this application will help you. Or if you’ve ever been in a pickle and wondered how well my Titles, Descriptions, or Keywords Tags are optimized, then this application will help yuo also. Once you submit your Google XML sitemap the application will download all of the URL’s included in your XML Sitemap and then one URL at a time goes out and fetches the Title and Meta Information. It then runs the character count from this data against what is deemed by Yahoo as being a well formatted Title and Meta Tag by YSSP standards. Title 70 characters in length, Description 170 characters in length, and 12 Keywords. I took a look at the Google Webmaster rule set also and added in some padding.
So for example the application will color the character count number Green if the Title falls in-between 60 and 70 characters. Then Red if the count is over 70 characters and Purple if it’s under 60 characters. This color theme is used also for the Description and the Keywords.

The complete thresholds includes.
Title: >=60 <=70 Good; > 70 Red; < 60 Purple
Description: >=150 <=170 Good; > 170 Red; < 150 Purple
Keywords:   >=10 <=12 Good; > 12 Red; < 10 Purple

Google Sitemap Tool
Creating and submitting Sitemaps

Copyright © 2024 Joseph Tinsley

Theme by Anders NorenUp ↑