forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.php
More file actions
145 lines (117 loc) · 2.75 KB
/
Http.php
File metadata and controls
145 lines (117 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Thenbsp\Wechat\Bridge;
use GuzzleHttp\Client;
use Thenbsp\Wechat\Bridge\Serializer;
use Thenbsp\Wechat\Wechat\AccessToken;
use Doctrine\Common\Collections\ArrayCollection;
class Http
{
/**
* Request Url
*/
protected $uri;
/**
* Request Method
*/
protected $method;
/**
* Request Body
*/
protected $body;
/**
* Request Query
*/
protected $query = array();
/**
* Query With AccessToken
*/
protected $accessToken;
/**
* SSL 证书
*/
protected $sslCert;
protected $sslKey;
/**
* initialize
*/
public function __construct($method, $uri)
{
$this->uri = $uri;
$this->method = strtoupper($method);
}
/**
* Create Client Factory
*/
public static function request($method, $uri)
{
return new static($method, $uri);
}
/**
* Request Query
*/
public function withQuery(array $query)
{
$this->query = array_merge($this->query, $query);
return $this;
}
/**
* Request Json Body
*/
public function withBody(array $body)
{
$this->body = Serializer::jsonEncode($body);
return $this;
}
/**
* Request Xml Body
*/
public function withXmlBody(array $body)
{
$this->body = Serializer::xmlEncode($body);
return $this;
}
/**
* Query With AccessToken
*/
public function withAccessToken(AccessToken $accessToken)
{
$this->query['access_token'] = $accessToken->getTokenString();
return $this;
}
/**
* Request SSL Cert
*/
public function withSSLCert($sslCert, $sslKey)
{
$this->sslCert = $sslCert;
$this->sslKey = $sslKey;
return $this;
}
/**
* Send Request
*/
public function send($asArray = true)
{
$options = array();
// query
if( !empty($this->query) ) {
$options['query'] = $this->query;
}
// body
if( !empty($this->body) ) {
$options['body'] = $this->body;
}
// ssl cert
if( $this->sslCert && $this->sslKey ) {
$options['cert'] = $this->sslCert;
$options['ssl_key'] = $this->sslKey;
}
$response = (new Client)->request($this->method, $this->uri, $options);
$contents = $response->getBody()->getContents();
if( !$asArray ) {
return $contents;
}
$array = Serializer::parse($contents);
return new ArrayCollection($array);
}
}