2013-05-22 32 views
4

好的我想將一個PHP變量發送到另一臺服務器,這是我的代碼,php變量是一個IP地址。將變量發送到另一臺服務器並使用PHP獲得結果

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

基本上其他的服務器獲取IP地址,並返回一個變量,名爲說明什麼,我不清楚上是返回描述變量返回到服務器的最佳途徑。 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"; 
    } 
} 
+0

您不會爲此使用'header() –

+2

搜索捲曲 – Jonathan

+0

depe根據你的情況,你需要執行[server](http://www.php.net/manual/en/curl.examples.php)或[client](http://api.jquery.com/jQuery .get /)端請求到另一臺服務器。 'header()'不會發生這種情況。 – hexblot

回答

1

源自服務器可以使用(如菲爾交叉提及)的file_get_contents或捲曲:

$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: 
} 

使用頭( '位置:XXX');函數,你實質上是在強制PHP在源服務器上響應一個302重定向頭,它將客戶端發送到遠程服務器,但沒有從遠程服務器返回到原始端。

+0

嗨,謝謝,我添加了數據的內容。PHP頁面(添加到原來的帖子)到#做somehting這裏部分,但它不會返回任何東西? – user1691024

1

代碼,頭會簡單地將用戶重定向到該網站。如果您的服務器配置允許遠程文件訪問,您希望使用類似file_get_contents()的內容。

如果沒有,看看cURL

您可以從捲曲的回報搶內容,並處理它們的方式。

0

好吧,如果我們假設data.php只返回描述你可以使用

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

它應該做的工作,但使用curl是最好的選擇。

0

此片段使用JSON返回值,這將允許您在將來擴展時返回多個值。

我通常在地方JSON的使用XML,但它似乎是走出去的風格:-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); 
1

您可以使用兩種方法:

如果目標網頁的純輸出的描述,那麼你可以使用

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

如果沒有,你可以用捲髮這樣的:

// 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 
+0

嗨,謝謝,但我不知道如何將我的data.php頁面的內容添加到此?並沒有看到你如何得到回聲輸出 – user1691024

+0

你的data.php需要輸出類似於: 'description = description and stuff ...&other_info = other info and other stuff ..' 然後你可以echo '$ output ['description']'或'$ output ['other_info']' – 2013-05-23 06:05:35

相關問題