Category Archives: PHP

PHP Experiments and Snippets

Work In progress.

I’m currently working on a complete re-vamp of a website of my accountant. Here is his current website: http://sabeehcpacga.ca  and here is the dev site I’m working on : http://dev.sabeehcpacga.ca.

The new site like the old one is build on wordpress, and will be completely responsive and will be optimized for SEO.

The new site is still in development, and I am doing this part time on my own free time without a deadline, but I intend to finish it by next week; the bulk of the content at least.

WordPress “Duplicator” Plugin – the Miracle Code

Recently I had to create an ecommerce site using WooCommerce and after I had setup the bulk of it and configured it to my liking on my local host I was looking for a way to migrate it to a live server.  I had migrated WordPress sites before but this was the first time I had to do one with a WooCommerce setup.

There were a lot of tutorials out there that seemed to give instruction on what should be a seamless and easy process but it was anything but. It seems that most of those tutorials are simply for plain WordPress sites without any fancy configurations (like WooCommerce) I was able to move the basic WP site and its theme settings and appearance but not of my products or product categories transferred over.

WooCommerce had extensions available that allow you to export and then import your store but they all cost money or didn’t work the way I wanted to.

Enter “Duplicator”. OH – MY – GOD. This was the simplest move of a wordpress site I had ever done. It took the minimum amount of steps and the whole process was as painless as could possibly be. It will package your entire site up and then give you an installer file to use with the file. Then you just upload the package and the installer.php into your server, call the installer.php, enter your new site URL and file location and DB info and you’re done. It was like a WordPress Batman. I could not recommend it enough. Please visit their site, download the plugin, use it and spread the word.

[button url=”http://wordpress.org/plugins/duplicator/” background=”#0e0″ size=”4″ style=”default”] WordPress Duplicator Plugin [/button]

Getting Geolocation details and then using them to extract 1 Groupon Deal

Recently I’ve been trying out for a variety of roles and on a recent one they sent me a questionnaire with two PHP challenges. The first one, given here, asks us to use a web service to get a users Lat and long based on their IP and then use this to query groupon for 1 deal in their location. The format of the query will be in the fom of a GET variable called ip={THE USERS IP} for example

www.example.com/challenge1.php?ip=12.14.25.223

The result should be an XML file. I’ve solved the bulk of it below, but I still have to figure out how to respond when you have no deals found.

<?php
if(isset($_GET['ip'])){

	$location = file_get_contents('http://freegeoip.net/json/'.$_GET['ip']);
	$ip_json=json_decode($location,true);
	$lat = $ip_json&#91;"latitude"&#93;;
	$long = $ip_json&#91;"longitude"&#93;;
	if(strlen($lat)>1 && strlen($long)>1){		

		$groupon_url='http://partner-api.groupon.com/deals.json?tsToken=insert_tracking_token_here&lat='.$lat.'&lng='.$long.'&limit=1&offset=0';

		$gr_string = file_get_contents($groupon_url);
		$gr_json=json_decode($gr_string,true);


		$title = $gr_json["deals"][0]["title"];
		$desc = strip_tags($gr_json["deals"][0]["pitchHtml"]);
		$disp_url = $gr_json["deals"][0]["dealUrl"];
		$clk_url = $gr_json["deals"][0]["options"][0]["buyUrl"];

		//-----THIS GENERATES THE XML-----------//

		header('Content-type: text/xml');
		header('Pragma: public');
		header('Cache-control: private');
		header('Expires: -1');
		echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		echo '<xml>';
		echo '<results>';
		echo '<title>'.$title.'</title>';
		echo '<desc>'.$desc.'</desc>';
		echo '<display_url>'.$disp_url.'</display_url>';
		echo '<click_url>'.$clk_url.'</click_url>';
		echo '</results>';
		echo '</xml>';
	}
	else{
		echo "Could not resolve the ip into lat and long";
	}
}
else{
	header('Content-type: text/xml');
	header('Pragma: public');
	header('Cache-control: private');
	header('Expires: -1');
	echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
	echo '<xml>';
	echo '</xml>';
}

How to create a Captcha with PHP GD library.

The “Captcha” idea is brilliant. While they are not totally foolproof (certain Universities have successfully been able to create software that are able to hack captchas), they provide a decent bit of protection from spam activity. The recaptha tool that many of us have seen on various sites on the web is a particular favourite of mine because apart from the added protection it also allows the crowd-sourcing of data via this small action. Currently I’ve seen two variations of this crowd sourcing. One to digitize books, so you’ll be shown a word that the Google OCR engine was probably not able to recognize and then you help them know what the word is, and the other one I’ve seen is the house numbers of Google Maps Streetview, which would probably give them more the ability to more accurately pinpoint addresses in their Maps. Below I’ve included two files that I’ve used to create a very basic captha module using PHP’s GD library. Continue reading How to create a Captcha with PHP GD library.