Http::_post()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 42
Code Lines 30

Duplication

Lines 4
Ratio 9.52 %

Importance

Changes 0
Metric Value
cc 4
eloc 30
nc 6
nop 2
dl 4
loc 42
rs 8.5806
c 0
b 0
f 0
1
<?php
2
namespace OpenOauth\Core\Http;
3
4
class Http
5
{
6
    public static $error;
7
8
    public static function _post($apiUrl, $data)
9
    {
10
        $apiUrl = urldecode($apiUrl);
11
        $ch     = curl_init();
12
13
        if (is_array($data)) {
14
            if (!defined('JSON_UNESCAPED_UNICODE')) {
15
                // 解决php 5.3版本 json转码时 中文编码问题.
16
                $data = json_encode($data);
17
                $data = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $data);
18
            } else {
19
                $data = json_encode($data, JSON_UNESCAPED_UNICODE);
20
            }
21
        }
22
23
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
24
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
25
        curl_setopt($ch, CURLOPT_HEADER, true);
26
        curl_setopt($ch, CURLOPT_NOBODY, false);
27
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
28
        curl_setopt($ch, CURLOPT_POST, 1);
29
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
30
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
31
32
        $res       = trim(curl_exec($ch));
33
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
34
        curl_close($ch);
35
36
        $header = '';
37
        $body   = $res;
38 View Code Duplication
        if ($http_code == 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
            list($header, $body) = explode("\r\n\r\n", $res, 2);
40
            $header = http_parse_headers($header);
41
        }
42
43
        $result           = [];
44
        $result['info']   = $body;
45
        $result['header'] = $header;
46
        $result['status'] = $http_code;
47
48
        return self::packData($result);
49
    }
50
51
    public static function _get($apiUrl)
52
    {
53
        $apiUrl = urldecode($apiUrl);
54
        $ch     = curl_init($apiUrl);
55
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
56
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
57
        curl_setopt($ch, CURLOPT_HEADER, true);
58
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
60
61
        $res       = curl_exec($ch);
62
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
63
        curl_close($ch);
64
65
        $header = '';
66
        $body   = $res;
67 View Code Duplication
        if ($http_code == 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
            list($header, $body) = explode("\r\n\r\n", $res, 2);
69
            $header = http_parse_headers($header);
70
        }
71
72
        $result           = [];
73
        $result['info']   = $body;
74
        $result['header'] = $header;
75
        $result['status'] = $http_code;
76
77
        return self::packData($result);
78
    }
79
80
    /**
81
     * 对接口返回的数据进行验证和组装.
82
     *
83
     * @author Tian
84
     *
85
     * @date   2015-12-08
86
     *
87
     * @param array $apiReturnData 由_post|| _get方法返回的数据.
88
     *
89
     * @return array
90
     */
91
    private static function packData($apiReturnData)
92
    {
93
        $status     = $apiReturnData['status'];
94
        $returnData = $apiReturnData['info'];
95
96
        if ($status != 200 && empty($returnData)) {
97
            self::$error = '接口服务器连接失败.';
98
99
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by OpenOauth\Core\Http\Http::packData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
100
        }
101
102
        $apiReturnData = json_decode($returnData, true);
103
104
        if ($status != 200 && !$apiReturnData) {
105
            self::$error = $returnData;
106
107
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by OpenOauth\Core\Http\Http::packData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
108
        }
109
110
        if (isset($apiReturnData['errcode']) && $apiReturnData['errcode'] != 0) {
111
            $error = '错误码:' . $apiReturnData['errcode'] . ', 错误信息:' . $apiReturnData['errmsg'];
112
113
            self::$error = $error;
114
115
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by OpenOauth\Core\Http\Http::packData of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
116
        }
117
118
        return $apiReturnData;
119
    }
120
121
    //post 发送xml
122
    public static function _post_xml($url, $xml = null)
123
    {
124
        $ch = curl_init($url);
125
        curl_setopt($ch, CURLOPT_URL, $url);
126
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
127
        curl_setopt($ch, CURLOPT_POST, 1);
128
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
129
        $response = curl_exec($ch);
130
        if (curl_errno($ch)) {
131
            print curl_error($ch);
132
        }
133
        curl_close($ch);
134
135
        return $response;
136
    }
137
}