开发者

php - execute several other php-scripts from php

From a PHP script, is it possible to execute another PHP-script with different GET variables?

I have a script that basically works like this (pseudo code):

// GENERATE STUFF
$ids 开发者_C百科= fetch_from_database();

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

For several reasons, I need to get rid of wget, and do this locally. What's the best way to achieve the above, without wget?

I've tried include, but it doesn't like that the same file is included twice or something. Exec can't take $_GET variables.

Preferably, "php_script" shouldn't have to be edited at all.


Use curl http://ru2.php.net/manual/en/book.curl.php


I will copy & paste from my script where I am running some bots with it in my framework;

# 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

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;
 }

This code allows me to execute the script with CURL, and move on to running another bot.

You can loop the first code (above the function) so you could run multiple (& different) php scripts without waiting for response (or you could wait).

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜