|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CloudyCity\UCMarketingSDK\Kernel\Http; |
|
4
|
|
|
|
|
5
|
|
|
use function CloudyCity\UCMarketingSDK\Kernel\Support\csvStringToArray; |
|
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
7
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Response from easywechat. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Response extends GuzzleResponse |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getBodyContents() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->getBody()->rewind(); |
|
21
|
|
|
$contents = $this->getBody()->getContents(); |
|
22
|
|
|
$this->getBody()->rewind(); |
|
23
|
|
|
|
|
24
|
|
|
return $contents; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
|
29
|
|
|
* |
|
30
|
|
|
* @return Response |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function buildFromPsrResponse(ResponseInterface $response) |
|
33
|
|
|
{ |
|
34
|
|
|
return new static( |
|
35
|
|
|
$response->getStatusCode(), |
|
36
|
|
|
$response->getHeaders(), |
|
37
|
|
|
$response->getBody(), |
|
38
|
|
|
$response->getProtocolVersion(), |
|
39
|
|
|
$response->getReasonPhrase() |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Build to json. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function toJson() |
|
49
|
|
|
{ |
|
50
|
|
|
return json_encode($this->toArray()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Build to array. |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function toArray() |
|
59
|
|
|
{ |
|
60
|
|
|
$headers = $this->getHeaders(); |
|
61
|
|
|
$content = $this->getBodyContents(); |
|
62
|
|
|
|
|
63
|
|
|
if (!empty($headers['Content-Type']) && strpos($headers['Content-Type'][0], 'octet-stream')) { |
|
64
|
|
|
$content = iconv('gb2312', 'utf-8', $content); |
|
65
|
|
|
|
|
66
|
|
|
return csvStringToArray($content); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$array = json_decode($content, true, 512, JSON_BIGINT_AS_STRING); |
|
70
|
|
|
|
|
71
|
|
|
if (JSON_ERROR_NONE === json_last_error()) { |
|
72
|
|
|
return (array) $array; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return []; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get collection data. |
|
80
|
|
|
* |
|
81
|
|
|
* @return ArrayCollection |
|
82
|
|
|
*/ |
|
83
|
|
|
public function toCollection() |
|
84
|
|
|
{ |
|
85
|
|
|
return new ArrayCollection($this->toArray()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return object |
|
90
|
|
|
*/ |
|
91
|
|
|
public function toObject() |
|
92
|
|
|
{ |
|
93
|
|
|
return json_decode($this->toJson()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool|string |
|
98
|
|
|
*/ |
|
99
|
|
|
public function __toString() |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->getBodyContents(); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|