2017-10-13 100 views
-1

我需要運行在不同的服務器中的PHP代碼(我們服務器2稱呼它)從服務器1捲曲或的file_get_contents忽略輸出

在服務器1,我有這樣的事情

<?php 
file_get_contents('http://domain_in_server_2.com/php-script.php'); 
?> 

問題是,這個請求可能需要很長時間,我不需要輸出。我只想觸發腳本,而不必等待或獲取輸出。

無論如何要完成我想要的東西嗎?

非常感謝。

+0

這對'domain_in_server_2.com'的配置顯著依賴 - PHP腳本[可能會退出,如果客戶端斷開連接(HTTP ://php.net/manual/en/function.ignore-user-abort.php)。 – ceejayoz

+0

我想我的問題很清楚,我沒有使用瀏覽器。我的目標是觸發腳本,而不是關心它的工作/輸出。或者如何在您的評論 – aye

+0

中「斷開連接」file_get_contents('http ...')將用於'domain_in_server_2.com'的目的,就像通過瀏覽器訪問一樣。你的'file_get_contents'腳本可能**有**等待輸出。 – ceejayoz

回答

1

您可以使用套接字。見example

編輯:

這裏是從上面的鏈接代碼:

// Example: 
// post_async("http://www.server.com/somewhere",array("foo" => 123, "bar" => "ABC")); 

function post_async($url, $params) 
{ 
    // Build POST string 
    foreach ($params as $key => &$val) { 
     if (is_array($val)) $val = implode(',', $val); 
     $post_params[] = $key.'='.urlencode($val); 
    } 
    $post_string = implode('&', $post_params); 

    // Connect to server 
    $parts=parse_url($url); 
    $fp = fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno, $errstr, 30); 

    // Build HTTP query    
    $out = "$type ".$parts['path']." HTTP/1.1\r\n"; 
    $out.= "Host: ".$parts['host']."\r\n"; 
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n"; 
    $out.= "Content-Length: ".strlen($post_string)."\r\n"; 
    $out.= "Connection: Close\r\n\r\n"; 
    $out.= $post_string; 

    // Send data and close the connection 
    fwrite($fp, $out); 
    fclose($fp); 
} 
+0

非常感謝。我會嘗試看看它是否工作,並在這裏報告 – aye

+0

嗨@Joel,你的方法工作,服務器1中的腳本不會等待,但從服務器2如何提取發佈數據。 'file_put_contents('file.txt',$ _ POST [「foo」])'創建一個空的'file.txt'文件。非常感謝 – aye

+0

無論如何,我並不需要發佈數據,只是爲了讓問題更有用。我已經接受了答案。非常感謝你! – aye