1

如何在 PHP 脚本中模拟带有争论的脚本的命令行运行?或者那不可能?

换句话说,假设您有以下脚本:

#!/usr/bin/php
<?php
require "../src/php/whatsprot.class.php";

function fgets_u($pStdn) {
    $pArr = array($pStdn);

    if (false === ($num_changed_streams = stream_select($pArr, $write = NULL, $except = NULL, 0))) {
        print("\$ 001 Socket Error : UNABLE TO WATCH STDIN.\n");
        return FALSE;
    } elseif ($num_changed_streams > 0) {
        return trim(fgets($pStdn, 1024));
    }
}

$nickname = "WhatsAPI Test";
$sender =   ""; // Mobile number with country code (but without + or 00)
$imei =     ""; // MAC Address for iOS IMEI for other platform (Android/etc) 


$countrycode = substr($sender, 0, 2);
$phonenumber=substr($sender, 2);

if ($argc < 2) {
    echo "USAGE: ".$_SERVER['argv'][0]." [-l] [-s <phone> <message>] [-i <phone>]\n";
    echo "\tphone: full number including country code, without '+' or '00'\n";
    echo "\t-s: send message\n";
    echo "\t-l: listen for new messages\n";
    echo "\t-i: interactive conversation with <phone>\n";
    exit(1);
}

$dst=$_SERVER['argv'][2];
$msg = "";
for ($i=3; $i<$argc; $i++) {
    $msg .= $_SERVER['argv'][$i]." ";
}

echo "[] Logging in as '$nickname' ($sender)\n";
$wa = new WhatsProt($sender, $imei, $nickname, true);

$url = "https://r.whatsapp.net/v1/exist.php?cc=".$countrycode."&in=".$phonenumber."&udid=".$wa->encryptPassword();
$content = file_get_contents($url);
if(stristr($content,'status="ok"') === false){
    echo "Wrong Password\n";
    exit(0);
}

$wa->Connect();
$wa->Login();

if ($_SERVER['argv'][1] == "-i") {
    echo "\n[] Interactive conversation with $dst:\n";
    stream_set_timeout(STDIN,1);
    while(TRUE) {
        $wa->PollMessages();
        $buff = $wa->GetMessages();
        if(!empty($buff)){
            print_r($buff);
        }
        $line = fgets_u(STDIN);
        if ($line != "") {
            if (strrchr($line, " ")) {
                // needs PHP >= 5.3.0
                $command = trim(strstr($line, ' ', TRUE));
            } else {
                $command = $line;
            }
            switch ($command) {
                case "/query":
                    $dst = trim(strstr($line, ' ', FALSE));
                    echo "[] Interactive conversation with $dst:\n";
                    break;
                case "/accountinfo":
                    echo "[] Account Info: ";
                    $wa->accountInfo();
                    break;
                case "/lastseen":
                    echo "[] Request last seen $dst: ";
                    $wa->RequestLastSeen("$dst"); 
                    break;
                default:
                    echo "[] Send message to $dst: $line\n";
                    $wa->Message(time()."-1", $dst , $line);
                    break;
            }
        }
    }
    exit(0);
}

if ($_SERVER['argv'][1] == "-l") {
    echo "\n[] Listen mode:\n";
    while (TRUE) {
        $wa->PollMessages();
        $data = $wa->GetMessages();
        if(!empty($data)) print_r($data);
        sleep(1);
    }
    exit(0);
}

echo "\n[] Request last seen $dst: ";
$wa->RequestLastSeen($dst); 

echo "\n[] Send message to $dst: $msg\n";
$wa->Message(time()."-1", $dst , $msg);
echo "\n";
?>

要运行这个脚本,你需要进入命令行,进入文件所在的目录,然后输入类似php -s "whatsapp.php" "Number" "Message".

但是,如果我想完全绕过命令行并直接在脚本中执行此操作,以便我可以随时从我的 Web 服务器运行它,我该怎么做呢?

4

2 回答 2

0

首先,您应该使用getopt

在 PHP 中,它支持短格式和长格式。

使用演示记录在我链接到的页面上。在您的情况下,我怀疑您将难以检测 a 是否<message>包含在您的-s标签的第二个参数中。将消息作为自己选项的参数可能会更容易。

$options = getopt("ls:m:i:");

if (isset($options["s"] && !isset($options["m"])) {
  die("-s needs -m");
}

至于从 Web 服务器运行东西......好吧,您使用 and 将变量传递给命令行 PHP 脚本,但是您使用and从 Web 服务器传递getopt()变量。如果您能找到一种明智的方法将 $_GET 变量映射到您的命令行选项,那么您应该很高兴。$argv$_GET$_POST

请注意,在获取命令行脚本并通过 Web 服务器运行它时,还存在各种其他注意事项。许可和安全齐头并进,通常是彼此的反函数。也就是说,如果您打开权限以便允许它做它需要做的事情,您可能会在您的服务器上暴露甚至创建漏洞。我不建议您这样做,除非您更有经验,或者您不介意事情是否中断或被脚本小子攻击以 0wn 您的服务器。

于 2012-11-01T04:44:52.903 回答
-1

您正在寻找反引号,请参阅

http://php.net/manual/en/language.operators.execution.php

或者你可以使用 shell_exec()

http://www.php.net/manual/en/function.shell-exec.php

于 2012-11-01T02:52:44.457 回答