Replacing $_GET to make it work from command line

荒凉一梦 提交于 2020-01-05 08:04:10

问题


Currently I have this small script that makes it possible to check if a domain name is free. It works from the browser, when you type check.php?domain=xxxx.com and you see if it is free or not.

Because of $_GET is used it only works from a browser and not from command line.

The PHP manual says that I should use $argv or getopt() to achieve this. I have tried it but then my script stops working.

How can the following code be made to work from command line?

<?php
include_once('/home/xxx/API.php');  
$CClient = new XCApi();
$CClient->isAvailable();
$d = $_GET['domain'];
ob_implicit_flush(1);

for ($i = 0; $i < 60000; ++$i) {

$domainResult = $CClient->checkDomainAvailability( new XDomain( $d ) );

if ( $domainResult->getStatus() == "domain_available" ) {
    echo $i . ". Domain " . $d . " is free (checked: " . date("Y-m-d H:i:s") . ")<br />";

    $_GET['domain'] = $d;
    include_once('Register.php');
    exit;

} 
elseif ( $domainResult->getStatus() == "domain_unavailable" ) {
    echo $i . ". Domain " . $d . " is unavailable (checked: " . date("Y-m-d H:i:s") . ")<br />";
}
else {
    echo $i . ". Domain " . $d . " is unknown (checked: " . date("Y-m-d H:i:s") . ")<br />";
}
echo"<pre>";
print_r($domainResult);
echo"</pre>";
}
?>

回答1:


Change

$d = $_GET['domain'];

to:

$d = $argv[1];

...and call it at the command line like this:

php /path/to/script.php "www.domaintocheck.com"



回答2:


write another scripts that reads command args and puts them in a $_GET array, then include this file.

#!/...
<?php
$_GET = array(
'domain' => $argv[1]
);

include 'yourscript.php';

or just put that bit at the top of (a copy of) your file




回答3:


Replace...

$d = $_GET['domain'];

with...

$d = $argv[1];

for a command-line version.

http://php.net/manual/en/reserved.variables.argv.php




回答4:


This is a duplicate of this: PHP passing $_GET in linux command prompt

Another possible way in Linux is to use curl (change localhost to your server's domain name):

curl http://localhost/check.php?domain=xxxx.com

Finally, the simple-stupid way is to check if $_GET['domain'] or $argv[1] or getopt('domain:') is set.

In that way your script will work both from http request or from the command line.



来源:https://stackoverflow.com/questions/10472446/replacing-get-to-make-it-work-from-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!