tags:

views:

271

answers:

4

Hi, How can I query a particular website with some fields and get the results to my webpage using php?

let say website xyz.com will give you the name of the city if you give them the zipcode. How can I acehive this easliy in php? any code snap shot will be great.

+1  A: 

Do you mean something like:

include 'http://www.google.com?q=myquery'; ? or which fields do you want to get?

Can you be a bit more specific pls :)

fmsf
let say website xyz.com will give you the name of the city if you give them the zipcode. How can I acehive this easliy? any code snap shot will be great.
+4  A: 

If I understand what you mean (You want to submit a query to a site and get the result back for processing and such?), you can use cURL.

Here is an example:

<?php 
    // create curl resource 
    $ch = curl_init(); 

    // set url 
    curl_setopt($ch, CURLOPT_URL, "example.com"); 

    //return the transfer as a string 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    // $output contains the output string 
    $output = curl_exec($ch); 

    // close curl resource to free up system resources 
    curl_close($ch);
?>

You can grab the Lat/Long from this site with some regexp like this:

if ( preg_match_all( "#<td>\s+-?(\d+\.\d+)\s+</td>#", $output, $coords ) ) {
    list( $lat, $long ) = $coords[1];
    echo "Latitude: $lat\nLongitude: $long\n";
}

Just put that after the curl_close() function. That will return something like this (numbers changed):

Latitude: 53.5100
Longitude: 60.2200
Nick Presta
but this brings whole page, i just need specific info from the site , how would i do that
You can use regular expressions (see the preg_match functions) or you can parse the HTML (see the DOM HTML parser). You have to be more specific about what data will be returned and what you want to do with it.
Nick Presta
here is the website http://www.ip-adress.com/ip_tracer/ i want to get latitde and lognitude from there, how can i achieve that?
works great but this preg_match_all does not make sense to me, if you could explain that would be great and also longitude always comes as a positive integer how do i change that?
A: 

If you want to import the html to your page and analyze it, you probably want to use cURL. You have to have the extensions loaded to your page (it's usually part of PHP _ I think it has to be compiled in? The manual can answer that)

Here is a curl function. Set up your url like

$param='fribby';

$param2='snips';

$url="www.example.com?data=$param&data2=$param2";

function curl_page($url) { $response =false; $ch = curl_init($url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_FAILONERROR,true); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch,CURLOPT_TIMEOUT,30); $response = curl_exec($ch); curl_close($ch); return $response; }

$page_data=curl_page($url);

Then, you can get data out of the page using the DOM parsing or grep/sed/awk type stuff.

Alex JL
I got the whole page opened into my website how do parse specific info from that page
+1  A: 

You can use file_get_contents (and other similar fopen-class functions) to do this:

$result = file_get_contents("http://other-site.com/query?variable=value");
Nerdling