1 | <?php |
||||
2 | |||||
3 | namespace AlibabaCloud\Dybaseapi\MNS; |
||||
4 | |||||
5 | use AlibabaCloud\Client\Exception\ClientException; |
||||
6 | use AlibabaCloud\Client\Result\Result; |
||||
7 | use GuzzleHttp\Psr7\Response; |
||||
8 | |||||
9 | /** |
||||
10 | * Class HttpHelper |
||||
11 | * |
||||
12 | * @package AlibabaCloud\Dybaseapi\MNS |
||||
13 | */ |
||||
14 | class HttpHelper |
||||
15 | { |
||||
16 | /** |
||||
17 | * @var int |
||||
18 | */ |
||||
19 | public static $connectTimeout = 30; //30 second |
||||
20 | |||||
21 | /** |
||||
22 | * @var int |
||||
23 | */ |
||||
24 | public static $readTimeout = 80; //80 second |
||||
25 | |||||
26 | /** |
||||
27 | * @param $url |
||||
28 | * @param string $httpMethod |
||||
29 | * @param null $postFields |
||||
0 ignored issues
–
show
Documentation
Bug
introduced
by
Loading history...
|
|||||
30 | * @param null $headers |
||||
0 ignored issues
–
show
|
|||||
31 | * |
||||
32 | * @return Result |
||||
33 | * @throws ClientException |
||||
34 | */ |
||||
35 | 1 | public static function curl($url, $httpMethod = 'GET', $postFields = null, $headers = null) |
|||
36 | { |
||||
37 | 1 | $ch = curl_init(); |
|||
38 | 1 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod); |
|||
39 | 1 | curl_setopt($ch, CURLOPT_URL, $url); |
|||
40 | 1 | curl_setopt($ch, CURLOPT_FAILONERROR, false); |
|||
41 | 1 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|||
42 | 1 | curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($postFields) ? self::getPostHttpBody($postFields) : $postFields); |
|||
0 ignored issues
–
show
|
|||||
43 | |||||
44 | 1 | if (self::$readTimeout) { |
|||
45 | 1 | curl_setopt($ch, CURLOPT_TIMEOUT, self::$readTimeout); |
|||
46 | 1 | } |
|||
47 | 1 | if (self::$connectTimeout) { |
|||
48 | 1 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$connectTimeout); |
|||
49 | 1 | } |
|||
50 | //https request |
||||
51 | 1 | if (strlen($url) > 5 && stripos($url, 'https') === 0) { |
|||
52 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
53 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||||
54 | } |
||||
55 | 1 | if (is_array($headers) && 0 < count($headers)) { |
|||
0 ignored issues
–
show
|
|||||
56 | 1 | $httpHeaders = self::getHttpHearders($headers); |
|||
57 | 1 | curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders); |
|||
58 | 1 | } |
|||
59 | |||||
60 | 1 | $body = curl_exec($ch); |
|||
61 | 1 | libxml_disable_entity_loader(); |
|||
62 | 1 | $json = json_encode(simplexml_load_string($body, 'SimpleXMLElement', LIBXML_NOCDATA)); |
|||
0 ignored issues
–
show
It seems like
$body can also be of type true ; however, parameter $data of simplexml_load_string() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
63 | 1 | $response = new Response( |
|||
64 | 1 | curl_getinfo($ch, CURLINFO_HTTP_CODE), |
|||
65 | 1 | [], |
|||
66 | $json |
||||
67 | 1 | ); |
|||
68 | |||||
69 | 1 | if (curl_errno($ch)) { |
|||
70 | throw new ClientException( |
||||
71 | 'Server unreachable: Errno: ' . curl_errno($ch) . ' ' . curl_error($ch), |
||||
72 | 'SDK.ServerUnreachable' |
||||
73 | ); |
||||
74 | } |
||||
75 | 1 | curl_close($ch); |
|||
76 | |||||
77 | 1 | return new Result($response); |
|||
78 | } |
||||
79 | |||||
80 | /** |
||||
81 | * @param $postFildes |
||||
82 | * |
||||
83 | * @return bool|string |
||||
84 | */ |
||||
85 | public static function getPostHttpBody($postFildes) |
||||
86 | { |
||||
87 | $content = ''; |
||||
88 | foreach ($postFildes as $apiParamKey => $apiParamValue) { |
||||
89 | $content .= "$apiParamKey=" . urlencode($apiParamValue) . '&'; |
||||
90 | } |
||||
91 | |||||
92 | return substr($content, 0, -1); |
||||
93 | } |
||||
94 | |||||
95 | /** |
||||
96 | * @param $headers |
||||
97 | * |
||||
98 | * @return array |
||||
99 | */ |
||||
100 | 1 | public static function getHttpHearders($headers) |
|||
101 | { |
||||
102 | 1 | $httpHeader = []; |
|||
103 | 1 | foreach ($headers as $key => $value) { |
|||
104 | 1 | $httpHeader[] = $key . ':' . $value; |
|||
105 | 1 | } |
|||
106 | |||||
107 | 1 | return $httpHeader; |
|||
108 | } |
||||
109 | } |
||||
110 |