主要是利用sublime的自带编译系统
新建json
1 2 3 4 5 6 7 8 9 10
| { "mehtod": "GET", "url": "https://wx.iguojin.com/activity/astrazeneca/saveData", "query": { "name": "测试", "hospital": "一家医院", "phone": "13011111111" } }
|
为json建编译系统
1 2 3 4 5 6 7 8 9
| { "cmd": [ "php", "C:\\php\\bin\\phppost\\index.php", "$file" ], "working_dir": "$file_path", "file_regex": ".json$" }
|
cmd:执行的命令
working_dir:工作目录感觉可以去掉
file_regex:文件,以json结尾时使用
php请求
此处使用了guzzlehttp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| <?php require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client; use GuzzleHttp\Cookie\CookieJar; use GuzzleHttp\Middleware;
// 默认配置 const default_config = [ 'method' => 'GET', 'url' => '', 'cookies' => [], 'query' => [], 'json' => [], 'body' => '', 'formData' => '', 'debug' => false, 'multipart' => [], ];
// 读取文件 $sourcePath = $argv[1]; $sourceDir = dirname($sourcePath); if (!file_exists($sourcePath)) { die('no this file'); }
// 获取配置 $file = file_get_contents($sourcePath); $input = json_decode($file, true); if (json_last_error()) { die(json_last_error_msg()); }
$config = array_merge(default_config, $input); extract($config); if (!$url) { die('没有请求地址'); }
// 数组body if ($body && is_array($body)) { $json = array_merge($json, $body); }
// 其他body if ($body && !is_array($body)) { $req['body'] = $body; }
// 请求配置 $req = []; $req['query'] = $query; if ($json) { $req['body'] = json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $req['headers'] = [ 'Content-Type' => 'application/json', ]; }
if ($multipart) { foreach ($multipart as &$multi) { if ($multi['contents'] ?? false) { $multi['contents'] = fopen($multi['contents'], 'r'); } } $req['multipart'] = $multipart; }
// cookies $cookiePath = $sourceDir . '/cookie.php'; if (file_exists($cookiePath)) { $baseCookie = include_once $cookiePath; $cookies = array_merge($baseCookie, $cookies); } if ($cookies ?? null) { $req['cookies'] = CookieJar::fromArray($cookies, parse_url($url, PHP_URL_HOST)); }
// formData if ($formData) { $req['form_params'] = $formData; }
// 日志 $client = new Client; $clientHandler = $client->getConfig('handler'); $tapMiddleware = Middleware::tap(function ($request) use ($debug) { echo 'uri:' . PHP_EOL; echo (string) $request->getUri() . PHP_EOL; echo '---------' . PHP_EOL; if (!$debug) { return; } echo 'header:' . PHP_EOL; $headers = $request->getHeaders(); foreach ($headers as $name => $value) { echo $name . ": " . implode(", ", $value) . PHP_EOL; } echo '---------' . PHP_EOL;
echo 'body:' . PHP_EOL; $body = (string) $request->getBody(); if ($body) { echo $body . PHP_EOL; } echo '---------' . PHP_EOL; }); $req['handler'] = $tapMiddleware($clientHandler);
// 发送请求 $t1 = microtime(true); $res = $client->request($method, $url, $req); $t2 = microtime(true); echo 'response:' . PHP_EOL; echo $res->getBody() . PHP_EOL; echo '---------' . PHP_EOL; echo 'time:' . PHP_EOL; echo ($t2 - $t1) . 's'; echo PHP_EOL;
|
关于cookie
只要在和测试的同级放一个cookie.php数组,即可加载,实现登录逻辑等
结果
按下ctrl+b即可
比postman更适合程序员,便于修改参数
1 2 3
| 接口耗时:0.14481711387634s {"code":1,"data":"","msg":101,"wait":3,"url":""} [Finished in 0.2s]
|