2011-04-26 106 views
0

其他幾個的PHP腳本從PHP腳本,纔有可能執行另一個PHP腳本與不同的GET變量?PHP - 執行從PHP

我有一個腳本,基本上是這樣的(僞代碼):

// GENERATE STUFF 
$ids = fetch_from_database(); 

foreach($ids as $id) 
{ 
    $command = "wget http://someserver.com/php_script.php?id=$id > output_$id"; 
    exec($command); 
} 

由於多種原因,我需要擺脫wget的,而做到這一點本地。沒有wget,達到上述目的的最佳方法是什麼?

我試過include,但它不喜歡,同樣的文件被包含兩次或東西。 Exec不能採取$_GET變量。

優選地,「php_script」不應該在所有要被編輯。

回答

1

我會從我的腳本,我正在我的框架一些機器人用它複製粘貼&;

# Executing The Script 
$data = array(); 
$data['start'] = 0; 
$data['end'] = 20; 

$url = SITE_ROOT.CRONS_DIR.$found->path."/".$found->name.".php"; 

$response = curlPost($url,$data,3,TRUE); 

if ($response){ 
echo "<pre>"; 
echo htmlentities($response); 
} 

curlPost功能

function curlPost($url, $postArray = NULL, $timeout=2, $errorReport=FALSE) { 
    # PREPARE THE POST STRING 
    if ($postArray != NULL) { 
     $postString = ''; 
     foreach ($postArray as $key => $val) { 
      $postString .= urlencode($key) . '=' . urlencode($val) . '&'; 
     } 
     $postString = rtrim($postString, '&'); 
    } 

    # PREPARE THE CURL CALL 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL,   $url  ); 
    curl_setopt($curl, CURLOPT_HEADER,   FALSE  ); 
    curl_setopt($curl, CURLOPT_POST,   TRUE  ); 
    ($postArray != NULL) ? curl_setopt($curl, CURLOPT_POSTFIELDS,  $postString) : ''; 
    curl_setopt($curl, CURLOPT_TIMEOUT,  $timeout ); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE  ); 

    # EXECUTE THE CURL CALL 
    $htm = curl_exec($curl); 
    $err = curl_errno($curl); 
    $inf = curl_getinfo($curl); 

    # ON FAILURE 
    if (!$htm) { 
     # PROCESS ERRORS HERE 
     if ($errorReport) { 
      echo "CURL FAIL: {$url} TIMEOUT={$timeout}, CURL_ERRNO={$err}"; 
      echo "<pre>\n"; 
      var_dump($inf); 
      echo "</pre>\n"; 
      createLog("CURL FAIL: {$url} TIMEOUT={$timeout}, CURL_ERRNO={$err}"); 
     } 
     curl_close($curl); 
     return FALSE; 
    } 

    # ON SUCCESS 
    curl_close($curl); 
    return $htm; 
} 

此代碼可以讓我的捲曲度來執行腳本,並移動到運行的另一個機器人。

您可以循環第一個代碼(函數上方),以便您可以運行多個(不同的)PHP腳本而不必等待響應(或者您可以等待)。

我希望這會有所幫助。