4

OK I want to send a PHP variable to another server, this is my code, the php variable is an ip address.

 header("Location: http://www.domainname.com/bean/data.php?ip=$ip");

Basically the other server will get the ip address and return a variable called Description what i am unclear on is the best way to return the description variable back to the server.

code on data.php page

 $ip =$_GET['ip'];
 include("ipology.class.php");
 $ipology = new ipology( array($ip) );
 $out = $ipology->out();
 foreach( $out as $ip ) {
    if( is_array( $ip ) ) {
       $address = implode( ", ", (array) $ip['address'] );
       $descr = implode( ", ", (array) $ip['descr'] );
       echo "$descr";
    }
 }
4
  • You won't be using header() for this Commented May 22, 2013 at 14:02
  • 2
    search around for curl Commented May 22, 2013 at 14:03
  • depending on your situation, you need to perform a server or client side request to the other server. Neither of those happens with header(). Commented May 22, 2013 at 14:05
  • Like that you perform redirect. Check the answers, you must use curl or file_get_contents(). Commented May 22, 2013 at 14:10

5 Answers 5

3

Originating server could use (as Phil Cross mentions) file_get_contents or curl:

$response = file_get_contents('http://www.domainname.com/bean/data.php?ip='.$ip);
print_r( $response );

Remote server could use:

if ( isset( $_GET['ip'] ) && $_GET['ip'] ) {
  # do description lookup and 'echo' it out:
}

Using the header('location: xxx'); function, what you're essentially doing is forcing PHP on the originating server to respond with a 302 redirection header which will send the client to the remote server but there's no 'going back' from the remote server to the originating.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thanks, I added the contents of the data.php page (added to the original post) to the # do somehting here section but it doesn't return anything?
1

That header will simply redirect the user to that website. You want to use something like file_get_contents() if your server config allows for remote file access.

If not, look into cURL

You can grab the contents from the return of curl and process them that way.

Comments

1

You can use two methods:

If the only output of the target page is the description, then you can use

$description = file_get_contents("http://target.page?ip=xxx.xxx.xxx.xxx");

If not, you can use curl like this:

// Create Post Information
$vars = array(
'ip'=>'xxx.xxx.xxx.xxx',
'some_other_info'=>'xxx'
);


// urlencode the information if needed
$urlencoded = http_build_query($vars);

if( function_exists( "curl_init" )) { 
    $CR = curl_init();
    curl_setopt($CR, CURLOPT_URL, 'http://distantpage');
    curl_setopt($CR, CURLOPT_POST, 1);
    curl_setopt($CR, CURLOPT_FAILONERROR, true);
    curl_setopt($CR, CURLOPT_POSTFIELDS, $urlencoded );
    curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($CR, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($CR, CURLOPT_FAILONERROR,true);


    $result = curl_exec( $CR );
    $error = curl_error ( $CR );


    // if there's error
    if( !empty( $error )) {
            echo $error;
            return;
    }

    curl_close( $CR );

}

parse_str($result, $output);
echo $output['description'];  // get description

2 Comments

Hi Thanks, but i am not sure how i would add the contents of my data.php page in to this? and don't see how you get the echo output
your data.php need to output something like: description=description and stuff...&other_info=other info and other stuff.. and then you can echo $output['description'] or $output['other_info']
0

Well if we assume that data.php returns only the description you can use

echo file_get_contents("http://www.domainname.com/bean/data.php?ip=".$ip);

It should do the job, but using CURL is the best option.

Comments

0

This snippet uses JSON to return the value, this will allow you to return multiple values in the future if your requirements expand.

I typically use XML in place of JSON, but it seems that it is going out of style :-P

Let me know if this works for you.

<?php

$output = file_get_contents("http://www.domainname.com/bean/data.php?ip=$ip");

// This is what would go in data.php
$output = '{ "ip": "127.0.0.1", "description": "localhost" }';

$parsed = json_decode($output);

echo "Description for $parsed->ip is $parsed->description\n";

// var_dump($parsed);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.