11<?php
22
3- if (! function_exists ('tap ' )) {
3+ if (!function_exists ('tap ' )) {
44 /**
55 * Call the given Closure with the given value then return the value.
66 *
@@ -23,3 +23,69 @@ function tap($value, $callback = null)
2323 return $ value ;
2424 }
2525}
26+
27+ if (!function_exists ('sendRequest ' )) {
28+ /**
29+ * Http 请求
30+ *
31+ * @param string $url 请求网址
32+ * @param string $method 请求方式
33+ * @param array $params 请求参数
34+ * @param array $headers 请求头
35+ *
36+ * @return bool|mixed
37+ */
38+ function sendRequest (string $ url , string $ method = "GET " , array $ params = [], array $ headers = [])
39+ {
40+ $ method = strtoupper ($ method );
41+
42+ $ httpInfo = array ();
43+
44+ $ ch = curl_init ();
45+
46+ curl_setopt ($ ch , CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
47+ curl_setopt ($ ch , CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36 ' );
48+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 30 );
49+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 30 );
50+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
51+
52+ if (!empty ($ headers )) {
53+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers );
54+ }
55+
56+ if (stripos ($ url , "https:// " ) !== FALSE ) {
57+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , FALSE ); // 对认证证书来源的检查
58+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , FALSE ); // 从证书中检查SSL加密算法是否存在
59+ }
60+
61+ if ($ method === 'POST ' ) {
62+ curl_setopt ($ ch , CURLOPT_POST , true );
63+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , $ params );
64+ curl_setopt ($ ch , CURLOPT_URL , $ url );
65+ } else {
66+ if ($ params ) {
67+ if (is_array ($ params )) {
68+ $ params = http_build_query ($ params );
69+ }
70+ curl_setopt ($ ch , CURLOPT_URL , $ url . '? ' . $ params );
71+ } else {
72+ curl_setopt ($ ch , CURLOPT_URL , $ url );
73+ }
74+ }
75+
76+ $ response = curl_exec ($ ch );
77+
78+ if ($ response === FALSE ) {
79+ //echo "cURL Error: " . curl_error($ch);
80+ return false ;
81+ }
82+
83+ $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
84+
85+ $ httpInfo = array_merge ($ httpInfo , curl_getinfo ($ ch ));
86+
87+ curl_close ($ ch );
88+
89+ return $ response ;
90+ }
91+ }
0 commit comments