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.
精彩评论