Jump to content

http requests


AdRock

Recommended Posts

I have made a function that gets called depending if its trying to get the start or end co-ords.

 

I don't want to redirect to that page but just get the co-ords using some variables i'm putting in the url....it's actually using google maps.

Link to comment
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.

Link to comment
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.