Issues (2)

src/Xmly/Http/Client.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Xmly\Http;
4
5
use Xmly\Config;
6
7
final class Client
8
{
9 8
    public static function get($url, array $headers = array())
10
    {
11 8
        $request = new Request('GET', $url, $headers);
12 8
        return self::sendRequest($request);
13
    }
14
15 5
    public static function post($url, $body, array $headers = array())
16
    {
17 5
        $request = new Request('POST', $url, $headers, $body);
18 5
        return self::sendRequest($request);
19
    }
20
21 13
    private static function userAgent()
22
    {
23 13
        $sdkInfo = "XmlyPHP/" . Config::SDK_VER;
24
25 13
        $systemInfo = php_uname("s");
26 13
        $machineInfo = php_uname("m");
27
28 13
        $envInfo = "($systemInfo/$machineInfo)";
29
30 13
        $phpVer = phpversion();
31
32 13
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
33 13
        return $ua;
34
    }
35
36 13
    public static function sendRequest($request)
37
    {
38 13
        $t1 = microtime(true);
39 13
        $ch = curl_init();
40
        $options = array(
41 13
            CURLOPT_USERAGENT => self::userAgent(),
42 13
            CURLOPT_RETURNTRANSFER => true,
43 13
            CURLOPT_SSL_VERIFYPEER => false,
44 13
            CURLOPT_SSL_VERIFYHOST => false,
45 13
            CURLOPT_HEADER => true,
46 13
            CURLOPT_NOBODY => false,
47 13
            CURLOPT_CUSTOMREQUEST => $request->method,
48 13
            CURLOPT_URL => $request->url,
49
        );
50
        // Handle open_basedir & safe mode
51 13
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
52 13
            $options[CURLOPT_FOLLOWLOCATION] = true;
53
        }
54 13
        if (!empty($request->headers)) {
55 12
            $headers = array();
56 12
            foreach ($request->headers as $key => $val) {
57 12
                array_push($headers, "$key: $val");
58
            }
59 12
            $options[CURLOPT_HTTPHEADER] = $headers;
60
        }
61 13
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
62 13
        if (!empty($request->body)) {
63 5
            $options[CURLOPT_POSTFIELDS] = $request->body;
64
        }
65 13
        curl_setopt_array($ch, $options);
66 13
        $result = curl_exec($ch);
67 13
        $t2 = microtime(true);
68 13
        $duration = round($t2 - $t1, 3);
69 13
        $ret = curl_errno($ch);
70 13
        if ($ret !== 0) {
71
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
72
            curl_close($ch);
73
            return $r;
74
        }
75 13
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76 13
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
77 13
        $headers = self::parseHeaders(substr($result, 0, $header_size));
0 ignored issues
show
It seems like $result can also be of type true; however, parameter $string of substr() 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 ignore-type  annotation

77
        $headers = self::parseHeaders(substr(/** @scrutinizer ignore-type */ $result, 0, $header_size));
Loading history...
78 13
        $body = substr($result, $header_size);
79 13
        curl_close($ch);
80 13
        return new Response($code, $duration, $headers, $body, null);
81
    }
82
83 13
    private static function parseHeaders($raw)
84
    {
85 13
        $headers = array();
86 13
        $headerLines = explode("\r\n", $raw);
87 13
        foreach ($headerLines as $line) {
88 13
            $headerLine = trim($line);
89 13
            $kv = explode(':', $headerLine);
90 13
            if (count($kv) > 1) {
91 13
                $kv[0] = self::ucwordsHyphen($kv[0]);
92 13
                $headers[$kv[0]] = trim($kv[1]);
93
            }
94
        }
95 13
        return $headers;
96
    }
97
98 13
    private static function ucwordsHyphen($str)
99
    {
100 13
        return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
101
    }
102
}
103