开发者

php exec() replacment maybe with proc_open()

开发者 https://www.devze.com 2023-03-20 06:56 出处:网络
Hi by us开发者_JAVA百科ing the exec() command like this $cmd = \"php -q nah.php some args\"; echo `$cmd`;

Hi by us开发者_JAVA百科ing the exec() command like this

$cmd = "php -q nah.php some args";
echo `$cmd`;
echo "lalal";

But if the php file doesn't have access to exec() cmd and somehow I find that using exec() isn't safe.

Replacements I could find were popen() and proc_open()

I could do this in popen() without arguments but I also want to pass some arguments(variable) to the file.

How can I do this - provoke a file and pass arguments(variable) together ?


//   foo.php

    function foo(){

     $var1 = $_GET['var1'];
     $var2 = $_GET['var2']

     //do something by variables



    } 

// bar.php

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, "foo.php?var1=XXXXXX&var2=XXXXXXXX"); 

    //Also you could send your variables by post 
    //$data = array('var1' => 'XXXXXXXXXXXX', 'var2' => 'XXXXXXXXX');
    //curl_setopt($ch, CURLOPT_URL, 'foo.php');
    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($ch); 

    curl_close($ch);

    echo $output;

EDIT

Use this

    $postdata = http_build_query(
        array(
            'var1' => 'some content',
            'var2' => 'doh'
        )
    );
    // If use POST 
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/x-www-form-urlencoded',
            'content' => $postdata
        )
    );

    $context = stream_context_create($opts);
    $file = file_get_contents('foo.php', false, $context);   

    // USE GET 
   $opts = array('http' =>
        array(
            'method'  => 'GET',
            'header'=>"Content-Type: text/html; charset=utf-8"
        )
    );

    $context = stream_context_create($opts);
    $file = file_get_contents('foo.php?'.$data, false, $context); 

    echo $file;


As far as I know, the command line arguments are simply part of the command, so they go in the first parameter, just like any other program execution function.

0

精彩评论

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