Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a php script from another php script by using the shell?

I have a php file called sample.php with the following content:

<?php
echo "Hello World!";
?>

And what I want to do, is to run this php script using a second php script.

I think shell_exec could help me, but I don't know its syntax.

By the way, I want to execute this files with cpanel. So I have to execute the shell.

Is there any way to do this?

like image 282
Sanjay Rathod Avatar asked Dec 09 '13 05:12

Sanjay Rathod


2 Answers

If you need to write a php file's output into a variable use the ob_start and ob_get_contents functions. See below:

<?php
    ob_start();
    include('myfile.php');
    $myStr = ob_get_contents();
    ob_end_clean();
    echo '>>>>' . $myStr . '<<<<';
?>

So if your 'myfile.php' contains this:

<?php
    echo 'test';
?>

Then your output will be:

>>>>test<<<<
like image 81
Randy Avatar answered Oct 06 '22 19:10

Randy


You can use cURL for remote requests. The below is from php.net:

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Here's a good tutorial: http://www.sitepoint.com/using-curl-for-remote-requests/

Consider watching this YouTube video here as well: http://www.youtube.com/watch?v=M2HLGZJi0Hk

like image 37
James Binford Avatar answered Oct 06 '22 20:10

James Binford