Browse Source

模拟请求

gorden 5 months ago
parent
commit
e5bd7a306d
1 changed files with 25 additions and 20 deletions
  1. 25 20
      app/functions.php

+ 25 - 20
app/functions.php

@@ -28,29 +28,34 @@ if (!function_exists('http_post')) {
 if (!function_exists('http_post_json')) {
     function http_post_json($url, $params)
     {
-        $ch = curl_init();
-        $this_header = array('Content-Type: application/json;charset=UTF-8');
-        curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
-        curl_setopt($ch, CURLOPT_URL, $url);
-        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
-        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
+        try {
+            $ch = curl_init();
+            $this_header = array('Content-Type: application/json;charset=UTF-8');
+            curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
+            curl_setopt($ch, CURLOPT_URL, $url);
+            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
+            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
 
-        curl_setopt($ch, CURLOPT_POST, 1);
-        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
-        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
-        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
+            curl_setopt($ch, CURLOPT_POST, 1);
+            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
+            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
+            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
 
-        $output = curl_exec($ch);
-        curl_close($ch);
-        // 写日志
-        if (is_json($output)) {
-            \support\Log::info('模拟请求:', json_decode($output, true));
-        } else {
-            \support\Log::info('模拟请求,返回异常:', ['msg' => (string)$output]);
-        }
+            $output = curl_exec($ch);
+            curl_close($ch);
+            // 写日志
+            if (is_json($output)) {
+                \support\Log::info('模拟请求:', json_decode($output, true));
+            } else {
+                \support\Log::info('模拟请求,返回异常:', ['msg' => (string)$output]);
+            }
 
-        return $output;
+            return $output;
+
+        } catch (\Exception $e) {
+            \support\Log::info('模拟请求失败:', ['msg' => $e->getMessage()]);
+        }
     }
 }