Completed
Push — main ( 8f9aaa...dcb65b )
by huang
05:08 queued 12s
created

Client::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Xmly\Http;
4
5
use Xmly\Config;
6
7
final class Client
8
{
9 4
    public static function get($url, array $headers = array())
10
    {
11 4
        $request = new Request('GET', $url, $headers);
12 4
        return self::sendRequest($request);
13
    }
14
15
    public static function post($url, $body, array $headers = array())
16
    {
17
        $request = new Request('POST', $url, $headers, $body);
18
        return self::sendRequest($request);
19
    }
20
21 4
    private static function userAgent()
22
    {
23 4
        $sdkInfo = "XmlyPHP/" . Config::SDK_VER;
24
25 4
        $systemInfo = php_uname("s");
26 4
        $machineInfo = php_uname("m");
27
28 4
        $envInfo = "($systemInfo/$machineInfo)";
29
30 4
        $phpVer = phpversion();
31
32 4
        $ua = "$sdkInfo $envInfo PHP/$phpVer";
33 4
        return $ua;
34
    }
35
36 4
    public static function sendRequest($request)
37
    {
38 4
        $t1 = microtime(true);
39 4
        $ch = curl_init();
40
        $options = array(
41 4
            CURLOPT_USERAGENT => self::userAgent(),
42 4
            CURLOPT_RETURNTRANSFER => true,
43 4
            CURLOPT_SSL_VERIFYPEER => false,
44 4
            CURLOPT_SSL_VERIFYHOST => false,
45 4
            CURLOPT_HEADER => true,
46 4
            CURLOPT_NOBODY => false,
47 4
            CURLOPT_CUSTOMREQUEST => $request->method,
48 4
            CURLOPT_URL => $request->url,
49
        );
50
        // Handle open_basedir & safe mode
51 4
        if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
52 4
            $options[CURLOPT_FOLLOWLOCATION] = true;
53
        }
54 4
        if (!empty($request->headers)) {
55 4
            $headers = array();
56 4
            foreach ($request->headers as $key => $val) {
57 4
                array_push($headers, "$key: $val");
58
            }
59 4
            $options[CURLOPT_HTTPHEADER] = $headers;
60
        }
61 4
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
62 4
        if (!empty($request->body)) {
63
            $options[CURLOPT_POSTFIELDS] = $request->body;
64
        }
65 4
        curl_setopt_array($ch, $options);
66 4
        $result = curl_exec($ch);
67 4
        $t2 = microtime(true);
68 4
        $duration = round($t2 - $t1, 3);
69 4
        $ret = curl_errno($ch);
70 4
        if ($ret !== 0) {
71
            $r = new Response(-1, $duration, array(), null, curl_error($ch));
72
            curl_close($ch);
73
            return $r;
74
        }
75 4
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
76 4
        $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
77 4
        $headers = self::parseHeaders(substr($result, 0, $header_size));
78 4
        $body = substr($result, $header_size);
79 4
        curl_close($ch);
80 4
        return new Response($code, $duration, $headers, $body, null);
81
    }
82
83 4
    private static function parseHeaders($raw)
84
    {
85 4
        $headers = array();
86 4
        $headerLines = explode("\r\n", $raw);
87 4
        foreach ($headerLines as $line) {
88 4
            $headerLine = trim($line);
89 4
            $kv = explode(':', $headerLine);
90 4
            if (count($kv) > 1) {
91 4
                $kv[0] = self::ucwordsHyphen($kv[0]);
92 4
                $headers[$kv[0]] = trim($kv[1]);
93
            }
94
        }
95 4
        return $headers;
96
    }
97
98 4
    private static function ucwordsHyphen($str)
99
    {
100 4
        return str_replace('- ', '-', ucwords(str_replace('-', '- ', $str)));
101
    }
102
}
103