Posts Tagged ‘PHP’

Google URL Shortner API

Monday, January 24th, 2011
One of the most important Google Tool i use regularly is Google URL Shortner, this is very helpful when you work with Social networking sites especially, When we wanted to share an URL which is too long and can’t fit into the char limit, that time this tool becomes handy. Even though we had few URL Shortner utilities like bit.ly, tinyurl earlier, when Google launched it’s own tool, i started using this more. Even though bit.ly is also giving you the option to analyze your URL traffics and etc, I love to work with Google, since most of the time i’m logged into any of the Google services. So it’ll be easy to use goo.gl
Basically it takes the URL and convert into a small/tiny with very few characters. We can use tools like Unshort me to unshorten the URL which are shortern by bit.ly etc. This can be applied to goo.gl URL’s also. As like all other services, Google provides API for this also. The API details can be found here. As like Google’s other API’s we need to have the Key to use this API as well.
To get your API activated on your profile, Please visit here. In the left hand panel, you can get your Key created for your profile and you can use it on your code. You can use programming languages of your choice and get the JSON data parsed which is returned from Google API Service.
We’ll look at two examples, of how to make a call to URL Shortner API. One with PHP and other one with Java.
Using PHP to Call API Service
			<?php

				$myURL = 'http://www.dzone.com/links/index.html';
				$googleAPIKey = 'URAPIKey';

				$requestData = array('longUrl' => $myURL, 'key' => $googleAPIKey);
				$jsonDetails = json_encode($requestData);

				$curl = curl_init();

				curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
				curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonDetails);
				curl_setopt($curl, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
				curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

				$responseData = curl_exec($curl);

				$jsonData = json_decode($responseData);

				curl_close($curl);

				echo 'Shortened URL is '.$jsonData->id;

			?>
		
Code in Action,Click Here
Using Java to Call API Service
I have used the basic “HttpsURLConnection”, but still you have plenty of other options to call Google’s API using Java.
			package com.techmaddy;

			import java.io.BufferedInputStream;
			import java.io.PrintWriter;
			import java.net.URL;
			import javax.net.ssl.HttpsURLConnection;

			public class URLShortnerDemo {

				public static void main(String[] args) {
					System.out.println("The URL is "+getShortURL());
				} //End of main

				public static String getShortURL() {
					URL simpleURL = null;
					HttpsURLConnection url = null;
					BufferedInputStream bStream = null;
					StringBuffer resultString = new StringBuffer("");
					String inputString = "{\"longUrl\":\"" + "http://news.google.com.sg/news?cf=all&ned=in&hl=en&edchanged=1" + "\"}";
					try {
						simpleURL = new URL("https://www.googleapis.com/urlshortener/v1/url?key=yourAPIKey");
						url = (HttpsURLConnection)simpleURL.openConnection();
						url.setDoOutput(true);
						url.setRequestProperty("content-type", "application/json");
						PrintWriter pw = new PrintWriter(url.getOutputStream());
						pw.print(inputString);
						pw.close();
					}
					catch (Exception ex) {
					  return "Exception in Connecting to API";
					}

					  try {
						  bStream = new BufferedInputStream(url.getInputStream());
						  int i;
						  while ((i = bStream.read()) >= 0) {
							  resultString.append((char)i);
						  }
					  }
					  catch (Exception ex) {
							return "Exception in Reading Result";
					  }

					return resultString.toString();

				} //End of getShortURL

			} //End of URLShortnerDemo
		
Output for JAVA
Google URL Shortner API Java OP
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Online Artwork Approval

Wednesday, October 27th, 2010

We are a promotional product company which sells pens, mugs, bags, rulers etc printed with our customer’s message / logo on. Currently our designer designs the product makes amendments and then sends the customer a PDF proof for them to check spellings position content etc and then sign and fax it back to us before we send the item into production. We basically want to streamline this process and let the customer digitally sign the proof off.

The Features we would be looking for are as follows:

When a job is booked in we would enter the customer’s details if not already in the online database, and open up a new job under a job number. We then can send an email template to the customer asking for artwork for the order, the status of the order would initially be set as awaiting artwork. If we the status has not been changed to awaiting mock-up then an automatic artwork reminder email is sent after a specified number of days. Under the customer information they will have a unique customer number and then each order will have a unique job number.

Once we have received the artwork the designer would set this status to awaiting mock-up
Once the mock-up is uploaded the customer is automatically emailed and the status changes to waiting for amendments or approval.

Once the approval is uploaded there will be a box with a deadline of approval this will have a default number of days in it (e.g. 5days), but if the designer requires the approval more urgently he can overtype it to a specified number of days. If the approval is needed the same day he can type in 0 which stops any chase emails but puts it to the top of priority list.

Once the artwork is uploaded and an email is automatically sent to the customer if they do not approve or make amendments within a set number of days they will get an automatic reminder email after 2 days (template 1) and again after 4 days (template 2) an email is also sent to the designer to let him know the customer is close to the deadline. If they go over the number of days specified then an overdue email is sent (template 3).

The customer will receive an email with a link, where they will be taken to the artwork page of that specific order. If the customer is happy with the design then they can click a few tick boxes saying they have checked spelling, layout, colors etc and put their name and then click a button to approve. A confirmation box will be displayed to make sure they do want to approve the artwork – this feature needs to be date and time stamped in the database as well as taking a note of their IP address. An email will be sent both to the customer and to the designer saying that the artwork has been approved; also the status of the job will be changed to approved.

If the customer is not happy with the design then they can click amendment which will bring up a text box, in which the customer can type what they want changing and click send. This will then send an email to our designer with the amendment information. If the customer asks for an amendment this will automatically open a new page amendment 1 (job number-1) where the designer needs to upload the changed artwork and the status would change to awaiting amendment. The whole process of approval will then start again until the customer is happy with the design.

An overview page will be available to the designer with all the jobs on the go that are awaiting artwork, approval or amendments (probably colour coded whether we are waiting on the customer or the designer) and in order of priority (days left to approve).

A customer online portal will be available where they can login under their email address and password (customer number); there would be a list of jobs with the ones that have not been approved at the top in date order. The customer will have the ability to go through the history of their artwork approvals and amendments.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Simple File Upload Script in PHP

Wednesday, June 30th, 2010
Here goes my file upload component in PHP. It just uploads the file into the specified directory.Other thing i have added as of now is it’ll upload the files into the folder on the current month and checks for the file exists already or not, if it already exists then it’ll prefix the time in milliseconds in the file name.
	<?php

		/*
		*		Copyright 2010 - Kumarasamy
		*		kumarasamy@techmaddy.com
		*/

		class DocumentHandler {

			var $uploadDir;// Directory where the documents to be uploaded

			function prepateForUpload() {
				$this->uploadDir = '/var/www/uploads';
				$this->uploadDir = $this->uploadDir . "/" . date("Y_m");

				//Check the month folder is available, if not then create it.
				if(!is_dir($this->uploadDir))
					mkdir($this->uploadDir);

			} //End of setDetails

			function copyFile($_FILES,$fileCntrlName) {
				$resultArray = array();
				$uploadfile = $this->uploadDir . "/" . basename($_FILES[$fileCntrlName]['name']);

				//If the file exists already in the folder, append the millisecond in the file name.
				if(is_file($uploadfile)) {
					list($msec, $sec) = explode(".", microtime(true));
					$uploadfile = $this->uploadDir . "/" . $msec . $sec . basename($_FILES[$fileCntrlName]['name']);
				}

				if (move_uploaded_file($_FILES[$fileCntrlName]['tmp_name'], $uploadfile)) {
					$resultArray['error'] = '';
					$resultArray['displayMessage'] = 'File Uploaded Successfully!!!';
				}

				return $resultArray;
			} //End of copyFile

		} //End of DocumentHandler
	?>
	
The following things can be added,

  • Check for the file type
  • Check for the file Size
  • Scan the document using the shell commands
  • Check for the special chars in the file name
  • File name length check also can be added
Idea’s are accepted!!!
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Get Adobe Flash player