1 Star 0 Fork 10

晓峰好神奇 / phpcli

forked from vb2005xu / phpcli 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
cli.php 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
vb2005xu 提交于 2015-03-09 20:45 . ..
<?php
define('IN_CLI', PHP_SAPI === 'cli');
if ( IN_CLI )
{
set_time_limit(0);
for ($i = 1; $i < $_SERVER['argc']; $i++)
{
$arg = explode('=', $_SERVER['argv'][$i]);
if (count($arg) > 1 || strncmp($arg[0], '-', 1) === 0)
{
$_GET[ltrim($arg[0], '-')] = isset($arg[1]) ? $arg[1] : true;
}
$_REQUEST = array_merge($_REQUEST,$_GET);
}
require_once dirname(__FILE__) . '/unirest.class.php';
if ( empty($_REQUEST['url']) )
{
return CliConsole::output('error', '无效参数 url');
}
$url = trim($_REQUEST['url']);
$method = empty($_REQUEST['method']) ? 'get' : trim($_REQUEST['method']);
unset($_REQUEST['url']);
unset($_REQUEST['method']);
$response = http_request($method, $url, $_REQUEST);
$response_body = $response->body;
if ( is_array($response_body) && !empty($response_body))
{
$response_body = $response->body;
}
else
{
$response_body = $response->raw_body;
}
CliConsole::output('response_time', date('Y-m-d H:i:s', time()));
return CliConsole::output('response', $response_body);
}
else
{
// header("Content-Type: text/html;charset=utf-8");
header("Status: 404 Not Found");
exit;
}
class CliConsole
{
static function output($note, $msg)
{
$msg = print_r($msg,true);
if ( is_windows() )
{
$msg = iconv("UTF-8", "GBK", $msg);
}
fwrite(STDOUT, "[$note]: " . $msg . PHP_EOL);
}
}
/**
* if operating system === windows
*/
function is_windows()
{
return 'win' === strtolower(substr(php_uname("s"), 0, 3));
}
function http_request($method, $url, $params=array())
{
Unirest::verifyPeer(false);
Unirest::timeout(86400 * 360);// 修正缺省超时时间
$method = trim(strtolower($method));
switch ($method) {
case 'post':
$url .= '?' . time();
$response = Unirest::post($url, array(), $params);
break;
default:
$params['t'] = time();
$response = Unirest::get($url, array(), $params);
break;
}
return $response;
}
1
https://gitee.com/xiaofengxx/phpcli.git
git@gitee.com:xiaofengxx/phpcli.git
xiaofengxx
phpcli
phpcli
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891