使用PHP将变量发送到另一台服务器并获取结果

4

好的,我想要将一个 PHP 变量发送到另一台服务器,这是我的代码,PHP 变量是一个 IP 地址。

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

基本上,另一个服务器将获取 IP 地址并返回一个名为“Description”的变量。我不确定最好的方法是如何将描述变量返回到服务器。

data.php 页面上的代码:

 $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";
    }
 }

你不会使用 header() 来完成这个。 - John Conde
2
搜索curl相关信息 - Jonathan DS
根据您的情况,您需要在服务器端或客户端向另一个服务器发出请求。这两种情况都不适用于header()方法。请参考此链接 - Nick Andriopoulos
像这样执行重定向。检查答案,你必须使用curl或file_get_contents()。 - V.Vachev
5个回答

3

作为Phil Cross所提到的,原始服务器可以使用file_get_contents或curl:

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

远程服务器可以使用:

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

使用header('location: xxx')函数,实际上是强制PHP在源服务器上响应302重定向标头,从而将客户端发送到远程服务器,但是无法从远程服务器返回到源服务器。

嗨,谢谢。我将data.php页面的内容(添加到原始帖子中)添加到“在此处执行某些操作”部分,但它没有返回任何内容? - user1691024

1
你可以使用两种方法:
如果目标页面的唯一输出是描述,则可以使用
```python response.css('::text').get() ```
或者
```python response.xpath('string()').get() ```
来提取描述内容。
$description = file_get_contents("http://target.page?ip=xxx.xxx.xxx.xxx");

如果不行,你可以像这样使用curl:
// 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

嗨,谢谢,但我不确定如何将我的data.php页面的内容添加到这里?我也看不到你是如何得到echo输出的。 - user1691024
你的data.php需要输出类似以下格式的数据: description=描述和其他内容...&other_info=其他信息和其他内容...然后你就可以使用echo $output['description']或者echo $output['other_info'] - user2050393

1
那个标题将简单地将用户重定向到该网站。如果您的服务器配置允许远程文件访问,您可以使用类似 file_get_contents() 的东西。
如果不行,请查看 cURL 您可以从curl的返回中获取内容并以这种方式处理。

0

如果我们假设data.php仅返回描述,那么您可以使用:

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

这样做可以完成任务,但使用CURL是最佳选择。


0

这个片段使用JSON返回值,如果您的需求扩展,这将允许您返回多个值。

通常我会使用XML代替JSON,但似乎它正在过时 :-P

如果这对您有用,请告诉我。

<?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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接