Pass a variable to another server and get the result using PHP

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

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

Basically, another server will get an ip address and return a variable called Description, which I am unclear is the best way to return the description variable back to the server.

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
source share
5 answers

The source server can 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 ); 

The remote server can use:

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

Using the header ('location: xxx'); function, what you essentially do is force PHP on the source server to respond with a 302 redirect header that will send the client to the remote server, but there is no "return" from the remote server to the source.

+2
source

This header will simply redirect the user to this website. You want to use something like file_get_contents() if your server configuration allows remote file access.

If not, check out cURL

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

+1
source

You can use two methods:

If the only result of the landing page is a description, then you can use

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

If not, you can use curl, for example:

 // 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 error if( !empty( $error )) { echo $error; return; } curl_close( $CR ); } parse_str($result, $output); echo $output['description']; // get description 
+1
source

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

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

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

0
source

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

I usually use XML instead of JSON, but it seems to go 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); 
0
source

Source: https://habr.com/ru/post/1482174/


All Articles