Jump to content

http requests


AdRock

Recommended Posts

I have form which i get the user to supply a street address and a town/city and I want to send that to google maps so i can get teh latitude/longitude co-ordinates

 

I know how to put those post variables into the url but how do i send an http request?

 

Say I wanted to call a link to http://www.somesite.com?this=that&that=this, how would i do that?

Link to comment
https://forums.phpfreaks.com/topic/96234-http-requests/
Share on other sites

You will need to use....

 

<?php $page = file_get_contents("http://www.somesite.com?this=that&that=this"); ?>

 

Then parse $page for the information you want.

 

PS: I'm sure google maps has an API that you could use to do this.

 

Google does have an api but is says

To access the Maps API geocoder directly using server-side scripting, send a request to http://maps.google.com/maps/geo? with the following parameters in the URI:

I can put the parameters in the URI but i need to sned a request

Link to comment
https://forums.phpfreaks.com/topic/96234-http-requests/#findComment-492794
Share on other sites

You need to get Google API key, then you could do:

 

<?php
$adress =  '1600+Amphitheatre+Parkway,+Mountain+View,+CA';
$output = 'json';
$api_key = 'xxx'; // your secret api key
$url = sprintf('http://maps.google.com/maps/geo?q=%s&output=%s&key=%s', $adress, $output, $api_key);

$info = json_decode(file_get_contents($url));
var_dump($info);
?>

 

Example here:

 

REMOVED - SERVER IS UNDER MAINTENANCEp

Link to comment
https://forums.phpfreaks.com/topic/96234-http-requests/#findComment-492806
Share on other sites

I once wrote this kind of geocoder myself (using Google Maps):

 

Posting from a from with input field 'q' containing the location to search for.

<?php
	// Google Maps API key
	$key = '';
	$url = 'http://maps.google.com/maps/geo?q='.urlencode($_POST['q']).'&key='.$key.'&output=csv';
	$data = explode(',', file_get_contents($url));
	// Searched string
	$location = $_POST['q'];
	$httpstatuscode = $data[0];
	$accuracy = $data[1];
	// Accuracy levels specified by Google. $accuracy_array[$accuracy] will return the readable version of the accuracy
	$accuracy_array = array(
		'Unknown location',
		'Country',
		'Region (state, province, prefecture, etc.)',
		'Sub-region (county, municipality, etc.)',
		'Town (city, village)',
		'Post code (zip code)',
		'Post code (zip code)',
		'Intersection',
		'Address'
	);
	// Comma separated latitude and longitude
	$latlong = $data[2].', '.$data[3];
?>

Link to comment
https://forums.phpfreaks.com/topic/96234-http-requests/#findComment-492810
Share on other sites

Many thanks thebadbad

 

I just edited it slightly and it seems to work though i haven't tried using my api key yet.  Ijust changed the url to a csv file i have on here to see if I can get it to poplulate the form fields.

 

Does anybody know if it would be possible to put it into a class and create a new instance of it with the post variables form the form being passed to it?

 

I did something like this

 

<?php

class Geocode {
function getcoords($street, $town, $postcode) {
	// Google Maps API key
	$key = '';
	$url = 'http://maps.google.com/maps/geo?q='.urlencode($location).'&key='.$key.'&output=csv';
	$data = explode(',', file_get_contents($url));
	// Searched string
	$location = $street.'+'.$town.'+'.$postcode;
	$httpstatuscode = $data[0];
	$accuracy = $data[1];
	// Accuracy levels specified by Google. $accuracy_array[$accuracy] will return the readable version of the accuracy
	$accuracy_array = array(
		'Unknown location',
		'Country',
		'Region (state, province, prefecture, etc.)',
		'Sub-region (county, municipality, etc.)',
		'Town (city, village)',
		'Post code (zip code)',
		'Post code (zip code)',
		'Intersection',
		'Address'
	);
	// Comma separated latitude and longitude
	$lat = $data[2];
	$long = $data[3];
}
}

but don't know how i would call it to be put back in my form

 

I currently do this

 

    function get_coords($street, $town, $postcode) {
global $lat,$long;

	// Google Maps API key
	$key = '';
	//$url = 'http://maps.google.com/maps/geo?q='.urlencode($location).'&key='.$key.'&output=csv';
	$url = 'test.csv';
	$data = explode(',', file_get_contents($url));
	// Searched string
	$location = $street.'+'.$town.'+'.$postcode;
	$httpstatuscode = $data[0];
	$accuracy = $data[1];
	// Accuracy levels specified by Google. $accuracy_array[$accuracy] will return the readable version of the accuracy
	$accuracy_array = array(
		'Unknown location',
		'Country',
		'Region (state, province, prefecture, etc.)',
		'Sub-region (county, municipality, etc.)',
		'Town (city, village)',
		'Post code (zip code)',
		'Post code (zip code)',
		'Intersection',
		'Address'
	);
	// Comma separated latitude and longitude
	$lat = $data[2];
	$long = $data[3];	
    }

    function get_start() {
global $lat,$long, $s_lat, $s_long;
get_coords($s_street, $s_town, $s_postcode);
//echo $lat." ",$long;
$s_lat = $lat;
$s_long = $long;
    }

 

the get_start function is called when i click the button to get the start co-ords

Link to comment
https://forums.phpfreaks.com/topic/96234-http-requests/#findComment-492873
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.