How to get Latitude/Longitude from an address (or Geocoding ) using PHP

March 10th, 2010 | Save to del.icio.us now(0)

While Google’s documents for maps API does good job in showing how to get lat/long from an address in JavaScript they do not really show any example of doing the same with PHP.

 

So to make you life simple here is a small script in PHP that does that.

 

 

$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address=573/1,+Jangli+Maharaj+Road,+Deccan+Gymkhana,+Pune,+Maharashtra,+India&sensor=false');

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;

 

The line above makes a request to Google maps API. Passes the address, and receives the response in JSON format.

The URL has following options

http://maps.google.com/maps/api/geocode/output?parameters

where output can be 1) JSON or 2) XML

For more details about parameters check out the Google’s geocoding documentation.


(Search web development related contents)

How To Fix MySQL Error – Error Code 30

December 3rd, 2009 | Save to del.icio.us now(0)

Yesterday was one of those days when things that can go wrong went wrong and today was MySQL day, and problem when accessing any table on a live site resulted in following error

MySQL Error - Can't create/write to file '/tmp/#sql_7d3f_0.MYI' (Errcode: 30)

It took me some time to figure out that the error was due to /usr/tmpDSK getting corrupted, and and not really a problem with MySQL or our database as we were thinking all this time, and as usual google came to rescue.

Here are the steps to fix this for cpanel users…  run the following commands in the order mentioned

/usr/sbin/lsof /tmp

/bin/umount -l /tmp

/bin/umount -l /var/tmp

/bin/rm -fv /usr/tmpDSK

/scripts/securetmp

This will create a new /tmp partition for you and the problem will go away. By the way error code 30 means that file system is read only.


(Search web development related contents)

Where is MySQL Gone Away?

November 11th, 2009 | Save to del.icio.us now(0)

Last Sunday I got to work on a very interesting problem in WordPress which I initially thought could be solved in like 5 mins, but alas it took me almost 7 hours before I found and fixed the problem.

Let me describe the problem, I was running a simple xml parsing script whose task was to parse the xml file and insert the content into WordPress database as a post, everything was working fine except the ‘INSERT’ statement was failing with out any errors. Basically everything would run but nothing would get inserted into database and no errors. We had used the ‘wp_insert_post’ function in  ‘post.php’ file to handle the insertion of post, which was returning ‘0’ instead.

After lot’s of time spend checking and cross checking the sql statements for error and PHP code logic, i finally found the problem which was a small kinda cryptic error ‘MySQL server has gone away’ for every single query that was getting executed in the script.

Well, a quick google search took me to MySQL manual page where it list bunch of possibilities on why the error might be coming.

To me the most logical one were

    1. You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.
    2. A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.
    3. You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (the reconnect flag in the MYSQL structure is equal to 0).
    4. You can also get these errors if you send a query to the server that is incorrect or too large.
    5. You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued.

I investigated each one but it turned out that because a query was taking a bit to long to execute MySQL closed the connection and refused all further request from the client.

So you might be wondering what was the Solution to this problem.

Well Rob of Rob’s notebook had the almost perfect solution for it. He created a replacement file for ‘wpdb.php’ which takes care of this problem, yeah it is temporary and you have to remember to replace this file every time you do an WordPress upgrade but it works.

If you are facing this problem go download it and replace you ‘wpdb’ file and save yourself some time.

Check out PHPCamp.net a article sharing website relevant to our own PHP community

(Search web development related contents)

Media Manager Browse Button Disabled In Joomla

August 19th, 2009 | Save to del.icio.us now(0)

Problem : No matter how many time you click on the media manager’s browse button nothing happens. It used to work few days back but not any more.

It is as if the the media manager buttons has been disabled.

media_manager_problem

If you were facing these problems it is most probably due to the Flash uploader not working properly with Flash version installed on your system.

Solution:

The simplest solution to fix this issue is to go to,

Global Configuration –> Systems –>Media settings.

And select ‘No’ for ‘Enable Flash Uploader’ setting, and save the settings.

mediamanager_config

That’s it, this simple. Now go back to you media manager and upload files to your heart’s content.


(Search web development related contents)

How to use SVN and Git together to get the best of both worlds in Windows

August 17th, 2009 | Save to del.icio.us now(0)

I have been using SVN to manage my source code for last three years, I can say I am quite happy with it, except for one problem, it was not possible to commit the code unless I was directly connected to my office network.

Then I heard about DVCS and Git. I found solution to one of my big problem, ability to commit code while I am not connected to office network, and share code with my teammates.

Their was only one small problem we didn’t wanted to loose the benefits of centralized repository, and we use Windows  OS( let’s not get into windows vs linux, i will win). So last 3-4 months I was looking for different DVCS systems with better windows support and i experimented with Mercurial (TrototoiseHg is good), but i think Git wins the race of DVCS hands down, and with popularity of GitHub I don’t see much choice here.

And finally I found about git-svn, and I was on my way.

Here are the commands and steps that you can use to manage Git and SVN together.

  1. Create a working copy(or actually a git repository ) using
    git svn clone –s <path to ur svn rep, eg https://akjoomgallery.googlecode.com/svn/trunk/>
  2. Get the source from svn repository to your git repository using
    git svn fetch

    (only needs to be done first time, it is slow and time consuming process)

  3. When you need to update your working copy  you use
    git svn rebase
  4. When you need to commit back to svn repository just use
    git svn dcommit

I used above commands in Git bash shell that comes with Git.

Also note their would be time when we have some changes in the Git repository that is not yet ready for commit, but we need to update our repository and Git won’t let you do that, without committing them (rebase will always give error) in such situation you use following set of commands

git stash

git svn rebase

git stash apply

git stash clear

Update:

And if you don’t want to these things manually here is my favourite tool that does that using nice GUI and perfect windows integration TortoiseGit

I have written this a month back before I found TortoiseGit, so this is now just for reference, and to get me back to my blogging.

Have fun.


(Search web development related contents)

Simple JavaScript ‘Frame Busting’ Code

April 16th, 2009 | Save to del.icio.us now(0)

Here is a quick JavaScript  code that you can use if you want to make sure that no one should be able to show your website in an IFrame.

<script type="text/javascript">
if (top !=self) {
   top.location=self.location;
}
</script>

Just put this code in head section of your html page, and it will ensure that your page always get’s displayed outside the frames.


(Search web development related contents)

Integrating With Twitter API In CodeIgniter

January 29th, 2009 | Save to del.icio.us now(0)

I needed to integrate Twitter API in one  of my web applications which uses CodeIgniter framework.

 

As their is no direct library available in CI for twitter integration, at first go this might look very difficult, but it is really simple.

 

Here is how you can do it..

  1. First download the  Twitter class for PHP.
  2. Put the class.twitter.php file in application/libraries folder.
  3. Rename class.twitter.php to twitter.php ( to make it compatible with CI naming conventions).
  4. Open twitter.php file and set following variables
var $username=’'; //twitter username
var $password=''; // twitter password
var $user_agent=''; //an identifier for twitter preferably email address
Are you satisfied with your knowledge? No, then spent 15 minutes every day on PHPCamp.net a knowledge sharing website for our own PHP community

If you have followed these steps your integration is almost complete, now all you need to do is use the twitter library, here is an example to get you started :)

 $this->load->library('Twitter');
 $msg=’Just Integrated Twitter with CodeIgniter ’;
 $this->twitter->update($msg);

By the way you can follow me on twitter @thecancerus.


(Search web development related contents)

Debugging PHP using Xdebug and Notepad++ : Part I

January 25th, 2009 | Save to del.icio.us now(0)

I am sure all of you have used ‘echo’ and ‘print_r’ to debug PHP.

We all know that this way debugging is hard and you need to remember to remove them from production server.

Well, thanks to xdebug you can now debug, and you don’t need expensive or blotted IDE for that, just plain and simple Notepad++ can do the job.open php.ini in wamp

In this post we will setup xdebug and DBGp plugin with Notepad++.

Let’s start by installing xdebug.

  1. Download the latest release of xdebug for PHP version you are using.
  2. Copy xdebug dll file into php’s extension directory, in my case, as I use wamp, it is c:\wamp\php\ext .
  3. Now we need to configure xdebug so that it get recognized by PHP, so open php.ini
  4. Add following at the end of your php.ini file
    [xdebug]
    zend_extension_ts="c:/wamp/php/ext/php_xdebug-2.0.3-5.2.5.dll"
    xdebug.profiler_output_dir = "c:/wamp/tmp/xdebug"
    xdebug.profiler_output_name = "cachegrind.out.%p"
    xdebug.profiler_enable = 0
    xdebug.profiler_append=0
    xdebug.extended_info=1
    xdebug.remote_enable=1
    xdebug.remote_handler=dbgp
    xdebug.remote_mode=req
    xdebug.remote_host=127.0.0.1
    xdebug.remote_port=9000
    xdebug.idekey=xdebug
    xdebug.remote_log="c:/wamp/tmp/xdebug/xdebug_remot.log"
    xdebug.show_exception_trace=0
    xdebug.show_local_vars=9
    xdebug.show_mem_delta=0
    xdebug.trace_format=0
  5. Just create a folder ‘xdebug’ in ‘c:\wamp\tmp’ folder.
  6. Finally restart Apache service .

With these steps you have, finished xdebug installation, it is configured for profiling application and remote debugging. Check documentation to know what all these configuration do, and tweak according to your preference.

Continue reading »


(Search web development related contents)

Step By Step Guide To Install Memcache On Linux

December 18th, 2008 | Save to del.icio.us now(0)

This post is more like a note to me, so that in future I can look up for steps involved in installing memcache on Linux servers like Centos or RHEL.

Those of you who follow me on twitter, will know that me and my friends spends hours trying to install memcache on one of our web server. Normally this is a five minute job, but unfortunately for us those tricks did not work out.

So if you have tried YUM and APT-GET and still could not install the memcache then read on, to find the alternate( read manual, without magic ) way of doing it.

When we talk about Memcache their two things that needs to be installed

  1. Memcache Daemon know as memcached, and
  2. Memcache client for your programing language, in this case PHP.

Continue reading »


(Search web development related contents)

Easy File Uploading Solution For PHP

September 1st, 2008 | Save to del.icio.us now(0)

Uploading files/images is a task that is required in almost all web applications.

While uploading file is a very simple task, still lot of beginners in PHP get stuck in this step. So in this post I decided to share the functions I learned while I was a beginner, and I still continue to use them because of it’s simplicity and usefulness.

For all my file uploading needs I use the class.upload.php written by Colin Verot. Ok, I know all about PHP functions like move_uploaded_file() and is_uploaded_file(), still I prefer to use class.upload.php file because, this file provides me with good set of image manipulation functions along with a neat way to know when a file has been uploaded.

So, let’s get started on, how to easily upload a file in PHP.

First, let’s create a simple HTML form for uploading files.

 <form action="file-upload.php" method="post" enctype="multipart/form-data">

  Upload file: <input type="file" name="fileupload">

                  <input type="submit" value="save">

 </form>

It is important to note that the form’s enctype is  multipart/form-data. This a place where novice developers make lot’s of mistake, they will forget to add enctype attribute to form and them spends hours thinking why they are not able to upload files or images.

Now, that we have a form to upload files, let’s see how we can handle file uploads on server.

I am going to cover four scenario’s here.

Continue reading »


(Search web development related contents)