MCPcopy Create free account
hub / github.com/CoderOpen/waimai / Curl

Class Curl

app/Http/Services/Net/Curl.php:18–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16use SebastianBergmann\CodeCoverage\Report\PHP;
17
18class Curl {
19 static function getData($url, $params = false, $ispost = 0, $header = [], $https = 0)
20 {
21 //dd($url, $params, $ispost);
22 $httpInfo = array();
23 $ch = curl_init();
24 curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
25 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');
26 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
27 curl_setopt($ch, CURLOPT_TIMEOUT, 30);
28 if ($header) {
29 $newHeader = [];
30 foreach ($header as $k => $v) {
31 $newHeader[] = $k . ':' . $v;
32 }
33 curl_setopt($ch, CURLOPT_HTTPHEADER, $newHeader);
34 }
35
36 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
37 if ($https) {
38 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
39 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
40 }
41 if ($ispost) {
42 curl_setopt($ch, CURLOPT_POST, true);
43 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
44 curl_setopt($ch, CURLOPT_URL, $url);
45 } else {
46 if ($params) {
47 if (is_array($params)) {
48 $params = http_build_query($params);
49 }
50 curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
51 } else {
52 curl_setopt($ch, CURLOPT_URL, $url);
53 }
54 }
55
56 $response = curl_exec($ch);
57
58 $error = curl_error($ch);
59
60 if ($response === FALSE || $error) {
61 return false;
62 }
63 //$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
64 //$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
65 curl_close($ch);
66 $result = json_decode($response, true) ? json_decode($response, true) : $response;
67 return $result;
68 }
69
70 /**
71 * 简单请求数据
72 */
73 static function guzzleRequest($url, $params, $timeout, $headers = [])
74 {
75 $client = new \GuzzleHttp\Client(['timeout' => $timeout, 'headers' => $headers, 'http_errors' => false,]);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected