在PHP中通过cURL请求源服务器域

时间:2011-08-17 11:17:47

标签: php curl

我有2台网络服务器,服务器A&服务器B.两者都运行PHP5 + Apache + Ubuntu环境。

服务器A通过PHP中的cURL向服务器B发送请求。我想获取请求的源服务器域。据我所知,$_SERVER['REMOTE_ADDR']可以获取源服务器的IP(服务器A)。如果我想获得服务器A的域名,我该如何获得它?

P.S。服务器A托管多个域,因此在这种情况下反向IP解析不起作用。

以下是代码:

$data = array('user' => $user, 'pass' => $pass);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ServerB/handler.php');
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$ans_xml = curl_exec($ch);

2 个答案:

答案 0 :(得分:2)

<?  
$data = array('user' => $user, 'pass' => $pass, 'appid' => 'pukeko');
$domain = $_SERVER["SERVER_NAME"]; // user the super global $_SERVER["SERVER_NAME"] or set it  manually to, ex: http://www.myserver.com 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ServerB/handler.php');
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_REFERER, $domain); // USE CURLOPT_REFERER to set the referer 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$ans_xml = curl_exec($ch);
?> 

<?
// ServerB - http://ServerB/handler.php
$referer = $_SERVER['HTTP_REFERER'];  // http://www.myserver.com 
?>

超级全局 $ _ SERVER [“SERVER_NAME”] 仅在您通过apache调用 scriptA 时才有效,例如:“ wget http://serverA/scritptA.php < /强>“

<强>更新

您还可以在帖子数据中发送$domain = $_SERVER["SERVER_NAME"]

$domain = $_SERVER["SERVER_NAME"]
$data = array('user' => $user, 'pass' => $pass, 'appid' => 'pukeko', 'icomefrom' => $domain);

并在http://ServerB/handler.php中获取:

$icomefrom = $_POST['icomefrom'];

这样你就不用担心假参赞了。

答案 1 :(得分:1)

在上面的评论中,作为统计数据Pelshoff,您应该使用自定义HTTP标头:

Custom HTTP headers : naming conventions