Completed
Push — master ( ce2690...7a8d5e )
by light
02:46
created

Api::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.1481

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 22
ccs 10
cts 15
cp 0.6667
rs 9.2
cc 2
eloc 14
nc 2
nop 1
crap 2.1481
1
<?php
2
3
/*
4
 * This file is part of the light/apistore.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\apistore\apis;
13
14
abstract class Api
15
{
16
    public $apikey;
17
18
    /**
19
     * @var bool If Response is json format, Default is true, will decode to array.
20
     */
21
    protected $isJsonResponse = true;
22
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @param string $apikey Api key
27
     */
28 17
    public function __construct($apikey)
29
    {
30 17
        $this->apikey = $apikey;
31 17
    }
32
33
    /**
34
     * Fetch result.
35
     *
36
     * @param mixed $params
37
     *
38
     * @return mixed
39
     */
40
    abstract public function get($params);
41
42
    /**
43
     * 准备请求头.
44
     *
45
     * @return array 默认的请头
46
     */
47 16
    protected function prepareHeaders()
48
    {
49
        return [
50 16
            'apikey:' . $this->apikey,
51 16
        ];
52
    }
53
54
    /**
55
     * Fetch result by $address.
56
     *
57
     * @param string $address
58
     *
59
     * @return mixed
60
     */
61 16
    public function fetch($address)
62
    {
63 16
        if (function_exists('curl_init')) {
64 16
            $ch = \curl_init();
65 16
            $header = $this->prepareHeaders();
66
            // 添加apikey到header
67 16
            \curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
68 16
            \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
69
            // 执行HTTP请求
70 16
            \curl_setopt($ch, CURLOPT_URL, $address);
71 16
            $res = \curl_exec($ch);
72 16
        } else {
73
            $context = stream_context_create([
74
                'http' => [
75
                    'header' => implode(';', $this->prepareHeaders()),
76
                ],
77
            ]);
78
            $res = file_get_contents($address, null, $context);
79
        }
80
81 16
        return $this->_parseResponse($res);
82
    }
83
84
    /**
85
     * Parse response to array.
86
     *
87
     * @param mixed $result
88
     *
89
     * @return array
90
     */
91 11
    protected function _parseResponse($result)
92
    {
93 11
        if (!$this->isJsonResponse) {
94 2
            return $result;
95
        }
96 9
        $result = json_decode($result, true);
97 9
        if (!$result || !isset($result['errNum'])) {
98
            return ['errcode' => 1, 'errmsg' => 'fetch data error'];
99
        }
100 9
        if ($result['errNum'] == 0) {
101
            return [
102 6
                'errcode' => 0,
103 6
                'errmsg' => 'success',
104 6
                'data' => $result['retData'],
105 6
            ];
106
        }
107
108 3
        return ['errcode' => $result['errNum'], 'errmsg' => $result['errMsg']];
109
    }
110
}
111