vijayjoshi.org - php | javascript | ajax | and all things web

Favorite tech links of the week : 10 Jan – 16 Jan, 2011

This post contains some of the useful and interesting tech links I found during this week. To keep up to date with the latest information, subscribe to the feed or follow me on twitter @v08i

22 Brilliant and Advanced jQuery Effects to WOW Your Visitors – http://goo.gl/IUz3d

22 jQuery Effects

12 Cool Javascript – jQuery checkbox replacement you shouldn’t miss – http://goo.gl/4OxcT

12 jQuery Checkboxes

70 Must See CSS3 Tips, Tricks And Tutorials – http://goo.gl/VarE4

70 Css Tricks

The anatomy of a WordPress theme – http://goo.gl/CCdLH

Wordpress Theme

Google URL Shortener API – http://goo.gl/Ia1fl

url Shortener

DOM Monster – http://goo.gl/nGySG

Dom Monster

Why You Should Never Search For Free WordPress Themes in Google or Anywhere Else – http://goo.gl/QSzTE

WP Theme

45 Cheat-Sheet Desktop Wallpaper For Web Designers and Developers – http://goo.gl/u1VT9

45 Cheat Sheets

What Makes a Good Website – http://goo.gl/Ua9ZW

Good Website

Listamatic – various types of lists with ul and CSS – http://goo.gl/EzlcX

Various Lists


WordPress Internal Server Error 500 in wp-admin

  • January 13th, 2011 Posted by Vijay Joshi in PHP | Tags:

If you are getting an internal server error 500 after installing wordpress while accessing admin, here is the fix.

Most likely it is caused by low memory limit on your server. To fix it, create a new file named php.ini in you wp-admin directory and put the following line in it.

memory_limit = 64M

Save the file, then reload the admin page and it will work fine.


PHP: Shorten URLs using Google URL shortener API

In past 2 posts you saw how short URLs can be expanded using the Google URL shortener API. This post shows how URLs can be shortened using this API.

API key is not mandatory for test purpose. Use this link to get an API key.

For shortening URLs, 2 points must be noted. First, a HTTP POST is required and the post data should be in JSON format. Here is the code:

//This is the URL you want to shorten
$longUrl = 'https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book';
$apiKey = 'your_api_key_here';
//Get API key from : http://code.google.com/apis/console/

$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);

$curlObj = curl_init();

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

$response = curl_exec($curlObj);

//change the response json string to object
$json = json_decode($response);

curl_close($curlObj);

echo 'Shortened URL is: '.$json->id;

Here is the JSON response from Google:

{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/lBfOH",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book"
}

You can also read these:


Using PHP and cURL to expand URLs using Google URL shortener API

Yesterday I wrote this post explaining how short goo.gl URLs can be expanded using the newly launched goo.gl API. In that code we used php function get_file_contents to get the response from API.

How about implementing the same functionality using cURL now? If you have libcurl support installed with PHP, you are ready to use the cURL functions of PHP.

Here is the code. API key is not mandatory but Google recommends that you use one. It will increase your per day usage limits too. Follow this link to get an API key.

$shortUrl = 'http://goo.gl/WlFX';
$apiKey = 'your_api_key_here';

$curlObj = curl_init();

curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl.'&key='.$apiKey);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);

$response = curl_exec($curlObj);

curl_close($curlObj);
$json = json_decode($response);

if($json->status === "OK")
{
	echo $json->longUrl;
}
else
{
	echo 'Bad luck.';
}

And here is the full JSON response:

{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/WlFX",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book",
 "status": "OK"
}

Am important point to note. Since the API is on https, set the value for CURLOPT_SSL_VERIFYPEER to false. This will stop cURL from verifying the SSL certificate.


PHP: Expanding URLs using Google URL shortener API

Yesterday. google launched an API for its URL shortening service http://goo.gl. Using this API developers will be able to expand/shorten URLs and get a user’s analytics and history programmaticaly.

Below is a small snippet of PHP code that expands a short URL. The response comes in JSON format, so we will use the json_decode function to convert it to object.

$shortUrl = 'http://goo.gl/WlFX';
$response = file_get_contents('https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl);
$json = json_decode($response);

if($json->status === "OK")
{
	echo $json->longUrl;
}
else
{
	echo 'Bad luck.';
}

Here is the full successfull response in JSON format.

{
 "kind": "urlshortener#url",
 "id": "http://goo.gl/WlFX",
 "longUrl": "https://www.packtpub.com/php-jquery-cookbook-to-create-interactive-web-applications/book",
 "status": "OK"
}

Finally, note that Google recommends the use of an API key. You can visit this link to read more about key and get a key from this link.

After using the key, your URL will look like the following:

$shortUrl = 'http://goo.gl/WlFX';
$apiKey = 'your_key_here';
$response = file_get_contents('https://www.googleapis.com/urlshortener/v1/url?shortUrl='.$shortUrl.'&key='.$apiKey);

Favorite tech links of the week : 03 Jan – 09 Jan, 2011

Comments Off

This post contains some of the useful and interesting tech links I found during this week. To keep up to date with the latest information, subscribe to the feed or follow me on twitter @v08i

32 Javascript Alternatives with Pure CSS – Updated – http://goo.gl/DNTvy

32JavascriptAlternatives

10 Google Chrome Extensions for a Faster Browser – http://goo.gl/slpTR

Chrome Extensions

75+ Tools for Visualizing your Data, CSS, Flash, jQuery, PHP – http://goo.gl/K8771

75 Tools

Click Here to Read full Article…