0

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 = 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.

2 Answers 2

3

Use curl https://www.php.net/manual/en/book.curl.php

Sign up to request clarification or add additional context in comments.

1 Comment

can you curl a local script, i.e. w/o using server address?
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.