Completed
Push — main ( dcb65b...a508c0 )
by huang
03:35
created

Client::userAgent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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