$app_file = __DIR__ . '/app/' . $app_name . '.php'; if (!is_file($app_file)) { throw new spWxException('Application file not found', 20005); } require $app_file; $available_ops = ['oauth', 'app']; if (!isset($_GET['op']) || !in_array($_GET['op'], $available_ops)) { throw new spWxException('OP 无效', 20006); } if (!isset($_GET['action'])) { throw new spWxException('A 无效', 20007); } $op = $_GET['op']; if ($op == 'oauth') { $obj = spWeixin::OAuth(); } else { if ($op == 'app') { $obj = spWeixin::App(); } else { throw new spWxException('错误的op', 20008); } } $action = $_GET['action']; if (!is_callable([$obj, $action])) { throw new spWxException('A 无效', 20009); } $params = isset($_POST['params']) ? (array) json_decode($_POST['params'], true) : []; $params = array_values($params); $result = call_user_func_array([$obj, $action], $params); echo json_encode(['code' => 0, 'data' => $result], JSON_UNESCAPED_UNICODE); exit;
<?php /** * 微信接口文件定义 * * 访问方式: http://wx.sskaje.me/api.php?app=example * * @author sskaje */ require __DIR__ . '/classes/weixin.inc.php'; spWxError::SetExceptionHandler(); if (!isset($_GET['app'])) { throw new Exception('Bad app', 10001); } $app_name = strtolower($_GET['app']); if (!preg_match('#^[a-z0-9]+$#i', $app_name)) { throw new Exception('Bad app', 10002); } $app_file = __DIR__ . '/app/' . $app_name . '.php'; if (!is_file($app_file)) { throw new Exception('Application file not found', 10003); } require $app_file; foreach ($GLOBALS['REQUEST_HANDLERS'] as $request_type => $handler_class) { if (!class_exists($handler_class) || !is_subclass_of($handler_class, 'spWxRequest')) { throw new Exception('Bad request handler ' . $handler_class, 10004); } spWxMessage::RegisterHandler($request_type, $handler_class); } spWeixin::Api(); exit;
# 启用Exception handler spWxError::SetExceptionHandler(); if (!isset($argv[2])) { usage(); } $app_name = strtolower($argv[1]); if (!preg_match('#^[a-z0-9]+$#i', $app_name)) { usage(); } $app_file = __DIR__ . '/app/' . $app_name . '.php'; if (!is_file($app_file)) { throw new spWxException('微信应用文件不存在', 30001); } require $app_file; $command = strtolower($argv[2]); $app = spWeixin::App(); if ($command == 'menu_create') { $menu_class = SPWX_MENU_CLASS; $ret = $app->menu_create(new $menu_class()); var_dump($ret); } else { if ($command == 'menu_get') { $ret = $app->menu_get(); var_dump($ret); } else { if ($command == 'menu_delete') { $ret = $app->menu_delete(); var_dump($ret); } } }