Smarter way to read $1 $2 in php

Hello,
I am running under ubuntu14.04 with php5 . When I run below php, it creates a token, then adds axaxax and bxbxbx into pc database, and at last, kills created token.
What I am trying to do is to add userid and password read from a file. I do not wish to enter username and password manually.
While surfing on google, I read that php does not read any variable from command line, according to being told though. So I gave up that method.
add_user.php

<?php
$szpc_ip="127.0.0.1";
$szAPIport="17000";
$szuserid="admin";
$szpassword="mypasswd";
// New User Profile
$szUsername="axaxax";
$szPassoword="bxbxbx";
$str=vsprintf("%s:%s",array($szuserid,$szpassword));
$strenc=base64_encode($str);
// Web API: createtokenbased64
$apiurl=vsprintf("http://%s:%s/token/createtokenbased64?encrpty=%s",array($szpc_ip,$szAPIport,$strenc));
$szreponse = file_get_contents($apiurl);
// Get token
$sztoken=substr($szreponse,6,strlen($szreponse)-8);
// Web API: add_user with token
$apiurl=vsprintf("http://%s:%s/server/add_user?token=%s&username=%s&password=%s",arr$
$szUsername,$szPassword));
$szuptime = file_get_contents($apiurl);
echo $szuptime;
// Web API: destroytoken with token
$apiurl=vsprintf("http://%s:%s/token/destroytoken?token=%s",array($szpc_ip,$szAPIport,$sztoken));
$szuptime = file_get_contents($apiurl);
?>

Instead above, I wrote a silly script creating text file, then I run it. Please do not laugh at me :slight_smile:

while read COL1 COL2; do
sed -i "s|axaxax|$COL1|g" add_user.php
sed -i "s|bxbxbx|$COL2|g" add_user.php
echo "php add_user.php $COL1 $COL2" > output
sleep 2
chmod 755 output
./output
sleep 2
sed -i "s|$COL1|axaxax|g" add_user.php
sed -i "s|$COL2|bxbxbx|g" add_user.php
sleep 60 #added due to token creation frequency is limited by the software
done<user_pass.txt
exit 0

user_pass.txt (tab seperated file)

franco franchi
ciccio ingrassia

This way, it's okay but not smart method.
Could you please lead me how to do it in php?

Thank you very much
Boris

Note, in PHP you read shell arguments to the PHP script using the $argv array.

You can test them as follows, for example:

<?php
echo $argv[0];
echo $argv[1];

Oh. I forgot to mention, you can also try getopt() :

getopt ( string $options [, array $longopts [, int &$optind ]] ) : array

Ref:

PHP: getopt - Manual

1 Like

Thank You Neo,
Solved now

Kind regards
Boris