2

I have a php file named test.php.In that file I have to take two inputs which are number and message.I wish to execute this php file from command prompt.The problem is that after executing the php file it should ask me for inputs but it isn't doing that.My code is below

echo "ENTER  YOUR NUMBER";
$num = $_GET["number"];
echo "Enter your message";
$msg = $_GET["message"];
echo $num;
echo $msg;

Can someone help me with it?

My test.php file now contains the following code

echo "ENTER  YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
$post['num'] = $num;
$post['msg'] = $msg;



$url = 'http://172.16.1.223/source%20codes/wc.php';

$fields = array(
'num'=>urlencode($post["num"]),
'msg'=>urlencode($post["msg"])

 );

$fields_string = http_build_query($fields);

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_POST,count($fields));
 curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
 $response = curl_exec($ch)

I am trying to access another file wc.php which is on another computer through ip address and using curl.Problem is that I am not understanding in wc.php file is the input taking format is correct or not.wc.php file contains the following code

$num=trim(fgets(STDIN));
$msg=trim(fgets(STDIN));
echo $num;
echo $msg;

I just want to pass the variables from test.php to wc.php

Afsana Khan
  • 122
  • 1
  • 12
  • 2
    $_GET is http GET arguments; cli has nothing to do with http; either use $argc/$argv for command line arguments, or read from stdin – Mark Baker Jan 11 '17 at 14:51

2 Answers2

6

you can do this:

 $input = trim(fgets(STDIN));

fgets takes input from STDIN and waits for a linebreak. The trim function makes sure you don't have that line break at the end.

So to add this in your example:

echo "ENTER  YOUR NUMBER";
$num = trim(fgets(STDIN));
echo "Enter your message";
$msg = trim(fgets(STDIN));
echo $num;
echo $msg;
Bert Bijn
  • 337
  • 1
  • 9
0

When working on the command line, you won't be able to use the superglobals ($_GET, $_REQUEST, etc.) like you can when using PHP on a website. You will want to pass the values in on the command line like this:

php -f myfile.php /number:"40" /message:"this is great"

You can then access the command line arguments with $argv.

raphael75
  • 2,982
  • 4
  • 29
  • 44