Category: PHP

Information and howto articles for PHP

PHP is a powerful open source server-sided scripting language that can be used on a wide variety of operating systems and webservers, including Apache on UNIX and IIS on Windows.

PHP contains numerous extensions which enable you to dynamically create images and flash files, parse xml, connect to remote servers using pop, imap, ftp, http and raw sockets, natively access many database servers including PostgreSql, MySQL and Microsoft SQL Server, compress and decompress files, lookup domain names and and much more.

PHP has an excellent manual and online documentation which covers every function for every extension available.

MAMP PHP cURL and SSL

I currently develop PHP based websites on a Mac using MAMP. When testing the Interspire Email Marketer API on a local version of a site posting data through to an https:// URL with cURL, I discovered that the version of cURL on MAMP has some issues with certs and SSL.

Read More »

PHP for loops with multiple statements in each expression

PHP for loops work in the same way as other programming languages and have a useful feature where each expression between the semi-colons can have multiple statements to be executed when separated by a comma. I found this useful myself when I need to have both a zero based and 1 based index in a loop.

Read More »

Google Analytics API PHP Class – Updates May 22nd 2009

I’ve made some updates to my Google Analytics API PHP Class today and posted the updated class and example files. The changes are documented here.





auth property is now public

The auth property which contains the authorization key after you’ve logged in is now a public property. You could therefore now do this:

$api1 = analytics_api();
 $api1->login($email, $password);
 
 $api2 = analytics_api();
 $api2->auth = $api1->auth;
 

You could also save the auth key into a database and use it for subsequent requests; once authorized it’s valid for several days and this can cut down initial request time.

data method start and end parameters

I’ve added the ability to specify the values “today”, “yesterday” and “week” as the $start parameter when calling the data method. “week” sets the start and end dates to be the 7 days before today. Leaving them as false defaults to the month before today (i.e. the same behaviour as previously).

filters parameter added to the data method

The $filters parameter has been added to the data method for specifying filters. When a value is passed in for this parameter it adds “&filters=$filters” to the request string. I haven’t used filters yet myself with the exception of a couple of tests when adding this option.

debug parameter added to the data method

If you set debug to true when calling data the url that is used to call the Analytics API is echoed out to standard output which is useful for seeing what’s going on behind the scenes. If run from the CLI will echo it along with a linebreak; otherwise will put it in a <p> tag and end with a newline.

get_summary method added

A new method get_summary was added to get summary information for a profile: number of visits, number of pageviews, average time on site and pages per visit. I’ll post more about this new function tomorrow.

get_summaries method added

Works the same as the above but gets summaries for all profiles you have access to.

sec2hms method added

This function is from http://www.laughing-buddha.net/jon/php/sec2hms/ and is used to format the seconds returned in the get_summary method into h:m:s format.

Read More »

PHP email validation with filter_var – updated

Just over a year ago I posted how to validate email addresses with PHP using filter_var instead of having to mess around with regular expressions. As pointed out in a comment, chris@example will pass validation; while that is actually a valid email address (the domain part of an email address doesn’t actually have to have dots in it, e.g. localhost) in most real situations we don’t want it to validate so this post adds a regular expression to the test to ensure the part after the @ contains a dot.

Read More »

Looping through the Google Analytics API PHP class data

Thanks for Jim Snyder from www.net-netanalytics.com for pointing out my examples were lacking the information to show how to loop through the resulting data from my Google Analytics API PHP Class. I have now update the examples file and also include the information about how to loop through the data in this post. You can always download the most recent version of my PHP class here.

The resulting array of data returned from the data() function is a multi dimensional array. The index(es) of the array are the dimensions passed in and then the value is another array with one vale for each of the metrics.

Looping through a single dimension

If, for example, you wanted to get the pagePath dimension and the pageviews and uniquePageviews metrics for pagePath, the data call would look like this:

$data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews');
 

You can then loop through the data like this:

foreach($data as $dimension => $metrics) {
     echo "$dimension pageviews: {$metrics['ga:pageviews']} unique pageviews: {$metrics['ga:uniquePageviews']}n";
 }
 

Looping through two dimensions

This example gets the browser and version as the dimensions and then visits and pageviews as the metrics. This requires an outer loop and an inner loop. The data call looks like this:

$data = $api->data($id, 'ga:browser,ga:browserVersion', 'ga:visits,ga:pageviews', false, false, false, 100);
 

The loop looks like this:

foreach($data as $dimension1 => $array) {
 foreach($array as $dimension2 => $metrics) {
         echo "$dimension1 $dimension2 visits: {$metrics['ga:visits']} pageviews: {$metrics['ga:pageviews']}n";
 }
 }
 

And some of the resulting output looks like this:

Internet Explorer 7.0 visits: 13486 pageviews: 16232
 Internet Explorer 6.0 visits: 4823 pageviews: 5932
 Internet Explorer 8.0 visits: 3047 pageviews: 3805
 Internet Explorer 999.1 visits: 15 pageviews: 15
 

You could also access the pageviews value directly for Internet Explorer 8 like this:

echo $data['Internet Explorer']['8.0']['ga:pageviews'], "n";
 
Read More »