PHP Classes

yahoo yes, google no

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  yahoo yes, google no  >  (Un) Subscribe thread alerts  
Subject:yahoo yes, google no
Summary:cannot post to google maps geocoder api
Messages:9
Author:Randy Ahn
Date:2006-08-23 04:25:12
Update:2007-05-10 21:48:58
 

  1. yahoo yes, google no   Reply   Report abuse  
Picture of Randy Ahn Randy Ahn - 2006-08-23 04:25:12
Trying to access google maps gecoding api but cannot get it to go. I am able to access the Yahoo geocoder api without a problem, but utilizing the same code with changes only in url and the arguments, the post appears to fail in passing the arguments to the google api (google gives you 50,000 requests/day, yahoo, a paltry 5,000). The response body is "{"name":"","Status":{"code":601,"request":"geocode"}}". Which is what you get when you try to access the script without any arguments: http://maps.google.com/maps/geo? Anyone have success with accessing google? Checked my key, and the only other arguments are output=xml and the address. Thanks.

  2. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-08-23 04:40:17 - In reply to message 1 from Randy Ahn
Maybe if you show the way you are using the class to access Google geocoder API, we can see what may be wrong.

  3. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Randy Ahn Randy Ahn - 2006-08-23 06:20:55 - In reply to message 2 from Manuel Lemos
Thx Manuel:
I used your example_http_post.php as a template:
require("http.php");

set_time_limit(0);
$http=new http_class;
$http->timeout=0;
$http->data_timeout=0;
$http->debug=0;
$http->html_debug=1;

$url="http://maps.google.com/maps/geo";
$error=$http->GetRequestArguments($url,$arguments);
$arguments["RequestMethod"]="POST";


//select addresses and submit to google

mysql_connect("localhost", "username", "password") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("myDb") or die(mysql_error());
echo "Connected to Database<br /><br />";
$sql = 'SELECT * FROM `agencies` LIMIT 0, 10 ';
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_array( $result )) {

$street = $row['Address1'];
$city = $row['City'];
$state = $row['State1'];
$zip = $row['ZIPCODE'];
$currentID = $row['ID'];
$address = $street.', '.$city.', '.$state.', '.$zip;
$address = ereg_replace(" ","+",$address);

$arguments["PostValues"]=array("q"=>$address,"key"=>"myKey", "output"=>"xml",
"MAX_FILE_SIZE"=>"1000000"
);

//then after the error checking, I output $body
echo HtmlSpecialChars($body);

//then I extract the coordinates and update my mysql db
}

  4. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-08-23 22:40:01 - In reply to message 3 from Randy Ahn
The request method must be GET, not POST.

The geocode XML request URL must be of the form:

$address = "1600 Amphitheatre Pky, Mountain View, CA";
$key = "key code here";
$url = "http://maps.google.com/maps/geo?q=" . UrlEncode($address) . "&key=".UrlEncode($key) . "&output=xml";

  5. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Randy Ahn Randy Ahn - 2006-08-24 05:13:39 - In reply to message 4 from Manuel Lemos
Thanks, I was able to get this to run cleanly. In addition to using GET, I had to move my while loop further up in the code in order to pass the arguments. I appreciate the help. I have used other scripts that you've authored and like this one, they have all worked flawlessly.

  6. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Connie Müller Connie Müller - 2007-05-10 12:07:00 - In reply to message 5 from Randy Ahn
Hi!

I am looking for a way to geocode adresses and write them into a MySQL-DB
Now I found this discussion.

Could you show me your program so I can find a way how to start? And not re-invent the wheel again?

Thanks in advance,

Connie

  7. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-05-10 14:02:48 - In reply to message 6 from Connie Müller
You can use HTTP client class to access the Google Maps API and get geocoding information. I do not have a ready to use example, but I am sure it is simple.

You also may want to search the site for other classes that query geocoding services.

  8. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Randy Ahn Randy Ahn - 2007-05-10 18:06:26 - In reply to message 7 from Manuel Lemos
Ok. Here's basically what I did -- it's messy; there may be some statements that aren't necessary within the while loop (I did not take the time to comment them all out as I only had to do the run one time) :) The usleep() function is important. Also, be sure to run a script to check for errors in the geocoding. Sometimes, google just can't geocode a legitimate address. hope this helps.


require("http.php");

mysql_connect("localhost", "usr", "pass") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("yourDB") or die(mysql_error());
echo "Connected to Database<br /><br />";
$sql = 'your sql';
$result = mysql_query($sql) or die(mysql_error());

//my while loop to select all addresses and submit to google
$count = "1";
while($row = mysql_fetch_array( $result )) {

$street = $row['Address1'];
$city = $row['City'];
$state = $row['State1'];
$zip = $row['ZIPCODE'];
$currentID = $row['ID'];
$key = "your google map api key";
echo '<h3>Agency ID = '.$currentID.'</h3>';
echo 'Record Count = '.$count.'<br />';
$address = $street.', '.$city.', '.$state.', '.$zip;
echo $address.'<br />';

set_time_limit(0);
$http=new http_class;
$http->timeout=0;
$http->data_timeout=0;
$http->debug=0;
$http->html_debug=1;

$url='http://maps.google.com/maps/geo?q='.urlencode($address).'&key='.urlencode($key).'&output=csv';

$error=$http->GetRequestArguments($url,$arguments);
$arguments["RequestMethod"]="GET";
$arguments["PostValues"]=array("q"=>$address,"key"=>"your google map api key", "output"=>"xml",
"MAX_FILE_SIZE"=>"1000000"
);
$arguments["PostFiles"]=array(
"userfile"=>array(
"Data"=>"This is just a plain text attachment file named attachment.txt .",
"Name"=>"attachment.txt",
"Content-Type"=>"automatic/name",
),
"anotherfile"=>array(
"FileName"=>"test_http_post.php",
"Content-Type"=>"automatic/name",
)
);
$arguments["Referer"]="http://www.alltheweb.com/";
$error=$http->Open($arguments);

if($error=="")
{
$error=$http->SendRequest($arguments);
if($error=="")
{
for(Reset($http->request_headers),$header=0;$header<count($http->request_headers);Next($http->request_headers),$header++)
{
$header_name=Key($http->request_headers);
if(GetType($http->request_headers[$header_name])=="array")
{
}
else
echo $header_name.": ".$http->request_headers[$header_name],"\r\n";
}
$headers=array();
$error=$http->ReadReplyHeaders($headers);
if($error=="")
{
for(Reset($headers),$header=0;$header<count($headers);Next($headers),$header++)
{
$header_name=Key($headers);
if(GetType($headers[$header_name])=="array")
{
for($header_value=0;$header_value<count($headers[$header_name]);$header_value++)
echo $header_name.": ".$headers[$header_name][$header_value],"\r\n";
}
else
echo $header_name.": ".$headers[$header_name],"\r\n";
}
echo "</PRE>\n";
flush();

echo "<b><LI>Response body:</LI</b>\n<PRE>\n";
for(;;)
{
$error=$http->ReadReplyBody($body,1000);
if($error!=""
|| strlen($body)==0)
break;
echo HtmlSpecialChars($body).'<br /><br />';

//extract coordinates
$coordinates = explode(",",$body);
$lat = $coordinates[2];
$lon = $coordinates[3];
echo 'latitude = '.$lat.'<br />';
echo 'longitude = '.$lon.'<br />';

//sometimes google times out -- this little hack will tell you if it does
if (eregi('[[:alpha:]]', $lat)) {
echo "google timing out";
break;
}
//echo '<h3>'.$counter.'</h3>';


// update your mysql tables
mysql_query("UPDATE yourtable SET lat='$lat',lon='$lon' WHERE ID='$currentID'")
or die(mysql_error());
echo '<br />mysql update successful!<br /><br />';
$count+='1';

}
echo "</PRE>\n";
//flush();
}
}
$http->Close();
}
// if(strlen($error))
// echo "<CENTER><H2>Error: ",$error,"</H2><CENTER>\n";

// sleep for google -- important -- you can't bombard google with your queries or they will ban you. Instead, use a usleep().
usleep(1750000);

//closing brace for while loop
}


echo '<h3>Run Complete!</h3>';


  9. Re: yahoo yes, google no   Reply   Report abuse  
Picture of Connie Müller Connie Müller - 2007-05-10 21:48:58 - In reply to message 8 from Randy Ahn
Thank you Manuel and Randy,

I will test at the weekend!

I am optimistic, thanks!

Connie