IMB
IMB

Reputation: 15889

How to run an external PHP script silently within a PHP script?

For example:

Content of server-a.com/test.php:

echo 'hello world one';

// now I need to call server-b.com/test.php here silently
// without interfering with user experience

echo 'hello world two';

Content of server-b.com/test.php:

mail($foo, $bar, $qux); header('location: http://google.com');


Now running server-a.com/test.php should output hello world one hello world two. And it should not redirect to google.com even though server-b.com/test.php was called successfully.

Upvotes: 0

Views: 7166

Answers (4)

Nanocom
Nanocom

Reputation: 3726

You can use Symfony2's Process component for that. Example:

use Symfony\Component\Process\PhpProcess;

$process = new PhpProcess(<<<EOF
    <?php echo 'Hello World'; ?>
EOF
);
$process->run();

Upvotes: 0

Musa
Musa

Reputation: 97672

You can remove the location header with header_remove. After you include server-b.com/test.php just call

header_remove('Location');

Upvotes: 0

ahmetunal
ahmetunal

Reputation: 3950

You can use file_get_contents()

file_get_contents("server-b.com/test.php");

or Curl

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "server-b.com/test.php"); 
curl_setopt($ch, CURLOPT_HEADER, TRUE); 
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_exec($ch); 

Upvotes: 5

Nwafor
Nwafor

Reputation: 184

use this function file_get_contents():

file_get_contents("server-b.com/test.php");

Upvotes: 0

Related Questions