웹 서버없이 명령 줄에서 PHP 스크립트를 실행할 수있다. 이를 위해이 스크립트에 다음과 같은 논리를 추가 :
STDIN가 정의하고 명령 줄 스위치를 처리 할 수
php myfile.php -s --longflag <argument>
스크립트가 PHP 인터프리터와 명령 줄에서 실행
if (defined('STDIN')) {
if (isset($argv)){
// handle your command line arguments here with getopt
}
}
// GET request parameter definitions //
else {
// handle your URL parameters (via GET or POST requests) here
}
, 플래그 , if 블록에 getopt과 함께 인수가 있습니다.
웹 서버의 URL로 스크립트에 액세스하면 스크립트가 else 블록에 도달합니다. 현재 가지고있는 PHP 코드는 해당 블록에 넣을 수 있습니다. 나는이 테스트를 위해 도움이 될 찾을
// Command line parameter definitions //
if (defined('STDIN')) {
// check whether arguments were passed, if not there is no need to attempt to check the array
if (isset($argv)){
$shortopts = "c:";
$longopts = array(
"xrt",
"xrp",
"user:",
);
$params = getopt($shortopts, $longopts);
if (isset($params['c'])){
if ($params['c'] > 0 && $params['c'] <= 200)
$count = $params['c']; //assign to the count variable
}
if (isset($params['xrt'])){
$include_retweets = false;
}
if (isset($params['xrp'])){
$exclude_replies = true;
}
if (isset($params['user'])){
$screen_name = $params['user'];
}
}
}
// Web server URL parameter definitions //
else {
// c = tweet count (possible range 1 - 200 tweets, else default = 25)
if (isset($_GET["c"])){
if ($_GET["c"] > 0 && $_GET["c"] <= 200){
$count = $_GET["c"];
}
}
// xrt = exclude retweets from the timeline (possible values: 1=true, else false)
if (isset($_GET["xrt"])){
if ($_GET["xrt"] == 1){
$include_retweets = false;
}
}
// xrp = exclude replies from the timeline (possible values: 1=true, else false)
if (isset($_GET["xrp"])){
if ($_GET["xrp"] == 1){
$exclude_replies = true;
}
}
// user = Twitter screen name for the user timeline that the user is requesting (default = their own, possible values = any other Twitter user name)
if (isset($_GET["user"])){
$screen_name = $_GET["user"];
}
} // end else block
:
여기 짧거나 긴 명령 행 옵션으로 URL 매개 변수를 처리하는 방법을 보여줍니다 내 프로젝트 중 하나의 예입니다. 희망이 도움이됩니다.