Procházet zdrojové kódy

fix(exception): 修复 curl 命令生成格式

runphp před 1 týdnem
rodič
revize
4821fcc44c
1 změnil soubory, kde provedl 11 přidání a 12 odebrání
  1. 11 12
      src/Exception/ExceptionHandle.php

+ 11 - 12
src/Exception/ExceptionHandle.php

@@ -79,33 +79,32 @@ class ExceptionHandle extends Handle
         try {
             $request = $this->app->request;
 
-            $curl  = PHP_EOL . '--- Curl Command ---';
+            $curl  = PHP_EOL . '--- Curl Command ---' . PHP_EOL;
 
             $method = $request->method();
             $url = $request->url(true);
+            $body = in_array($method, ['POST', 'PUT', 'PATCH']) ? $request->getInput() : null;
 
-            $curl .= PHP_EOL . 'curl';
-            if ($method !== 'GET') {
+            $curl .= 'curl';
+            // POST 带 body 时 curl 自动推断,其他非 GET 方法需要显式指定 -X
+            if ($method !== 'GET' && !($method === 'POST' && $body)) {
                 $curl .= ' -X ' . $method;
             }
-            $curl .= ' "' . $url . '"';
+            $curl .= " '" . $url . "'";
 
             // 请求头
             foreach ($request->header() as $name => $value) {
                 if ('' === $value) {
                     continue;
                 }
-                $curl .= ' \\' . PHP_EOL . '  -H "' . $name . ': ' . $value . '"';
+                $curl .= PHP_EOL . "-H '" . $name . ': ' . $value . "'";
             }
 
             // Body
-            if (in_array($method, ['POST', 'PUT', 'PATCH'])) {
-                $body = $request->getInput();
-                if ($body) {
-                    // 用单引号包裹 body,内部单引号需要转义
-                    $body = str_replace("'", "'\\''", $body);
-                    $curl .= ' \\' . PHP_EOL . "  -d '" . $body . "'";
-                }
+            if ($body) {
+                // 用单引号包裹 body,内部单引号需要转义
+                $body = str_replace("'", "'\\''", $body);
+                $curl .= PHP_EOL . "--data-binary '" . $body . "'";
             }
 
             return $curl;