好的,我想发送一个PHP变量到另一个服务器,这是我的代码,php变量是一个ip地址。
header("Location: http://www.domainname.com/bean/data.php?ip=$ip");基本上,另一个服务器将获得ip地址并返回一个名为Description的变量,我不清楚这是将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";
}
}发布于 2013-05-22 22:10:27
始发服务器可以使用(正如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:
}使用标头(‘location: xxx');函数,实际上是强制发起服务器上的PHP使用302重定向标头进行响应,该标头会将客户端发送到远程服务器,但不会从远程服务器返回到发起服务器。
发布于 2013-05-22 22:04:57
该标头将简单地将用户重定向到该网站。如果您的服务器配置允许远程文件访问,则需要使用类似于file_get_contents()的内容。
如果不是,请查看cURL
您可以从curl的返回中获取内容,并以这种方式处理它们。
发布于 2013-05-22 22:16:06
您可以使用两种方法:
如果目标页面的唯一输出是描述,那么您可以使用
$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 descriptionhttps://stackoverflow.com/questions/16693844
复制相似问题