|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mpyw\Cowitter\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use mpyw\Co\CURLException; |
|
6
|
|
|
use mpyw\Co\CoInterface; |
|
7
|
|
|
use mpyw\Cowitter\Response; |
|
8
|
|
|
use mpyw\Cowitter\Helpers\ResponseBodyDecoder; |
|
9
|
|
|
|
|
10
|
|
|
class ResponseYielder |
|
11
|
|
|
{ |
|
12
|
12 |
|
public static function syncExec($ch) |
|
13
|
12 |
|
{ |
|
14
|
12 |
|
if (false === $buffer = curl_exec($ch)) { |
|
15
|
1 |
|
throw new CURLException(curl_error($ch), curl_errno($ch), $ch); |
|
16
|
|
|
} |
|
17
|
11 |
|
$response = new Response($buffer, $ch); |
|
18
|
11 |
|
if ($response->getStatusCode() >= 300 && |
|
19
|
11 |
|
$response->getStatusCode() < 400 && |
|
20
|
11 |
|
'' !== $url = $response->getHeaderLine('Location')) { |
|
21
|
|
|
curl_setopt_array($ch, [ |
|
22
|
|
|
CURLOPT_URL => $url, |
|
23
|
|
|
CURLOPT_HTTPGET => true, |
|
24
|
|
|
CURLOPT_REFERER => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), |
|
25
|
|
|
]); |
|
26
|
|
|
return static::syncExec($ch); |
|
27
|
|
|
} |
|
28
|
11 |
|
return $response; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public static function asyncExec($ch) |
|
32
|
3 |
|
{ |
|
33
|
3 |
|
$response = new Response((yield $ch), $ch); |
|
34
|
3 |
|
if ($response->getStatusCode() >= 300 && |
|
35
|
3 |
|
$response->getStatusCode() < 400 && |
|
36
|
3 |
|
'' !== $url = $response->getHeaderLine('Location')) { |
|
37
|
|
|
curl_setopt_array($ch, [ |
|
38
|
|
|
CURLOPT_URL => $url, |
|
39
|
|
|
CURLOPT_HTTPGET => true, |
|
40
|
|
|
CURLOPT_REFERER => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), |
|
41
|
|
|
]); |
|
42
|
|
|
yield CoInterface::RETURN_WITH => static::syncExec($ch); |
|
43
|
|
|
} |
|
44
|
3 |
|
yield CoInterface::RETURN_WITH => $response; |
|
45
|
|
|
// @codeCoverageIgnoreStart |
|
46
|
|
|
} |
|
47
|
|
|
// @codeCoverageIgnoreEnd |
|
48
|
|
|
|
|
49
|
12 |
|
public static function syncExecDecoded($ch, $return_response_object = false) |
|
50
|
12 |
|
{ |
|
51
|
12 |
|
$response = static::syncExec($ch); |
|
52
|
11 |
|
$response = ResponseBodyDecoder::getDecodedResponse($response, $ch); |
|
53
|
4 |
|
if (!$return_response_object) { |
|
54
|
4 |
|
$response = $response->hasContent() ? $response->getContent() : null; |
|
55
|
|
|
} |
|
56
|
4 |
|
return $response; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
public static function asyncExecDecoded($ch, $return_response_object = false) |
|
60
|
3 |
|
{ |
|
61
|
3 |
|
$response = (yield static::asyncExec($ch)); |
|
62
|
3 |
|
$response = ResponseBodyDecoder::getDecodedResponse($response, $ch); |
|
63
|
3 |
|
if (!$return_response_object) { |
|
64
|
3 |
|
$response = $response->hasContent() ? $response->getContent() : null; |
|
65
|
|
|
} |
|
66
|
3 |
|
yield CoInterface::RETURN_WITH => $response; |
|
67
|
|
|
// @codeCoverageIgnoreStart |
|
68
|
|
|
} |
|
69
|
|
|
// @codeCoverageIgnoreEnd |
|
70
|
|
|
} |
|
71
|
|
|
|