1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bex\Behat\ScreenshotExtension\Driver\Service; |
4
|
|
|
|
5
|
|
|
use Buzz\Client\Curl; |
6
|
|
|
use Buzz\Message\Form\FormRequest; |
7
|
|
|
use Buzz\Message\Form\FormUpload; |
8
|
|
|
use Buzz\Message\Response; |
9
|
|
|
|
10
|
|
|
class UploadPieApi |
11
|
|
|
{ |
12
|
|
|
const REQUEST_URL = 'https://uploadpie.com/'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Curl |
16
|
|
|
*/ |
17
|
|
|
private $client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Curl|null $client |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Curl $client = null) |
23
|
|
|
{ |
24
|
|
|
$this->client = $client ?: new Curl(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $binaryImage |
29
|
|
|
* @param string $filename |
30
|
|
|
* @param int $expire |
31
|
|
|
* @param string $auth |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function call($binaryImage, $filename, $expire, $auth) |
36
|
|
|
{ |
37
|
|
|
$response = new Response(); |
38
|
|
|
|
39
|
|
|
$image = new FormUpload(); |
40
|
|
|
$image->setFilename($filename); |
41
|
|
|
$image->setContent($binaryImage); |
42
|
|
|
|
43
|
|
|
$request = $this->buildRequest($image, $expire, $auth); |
44
|
|
|
$this->client->setOption(CURLOPT_TIMEOUT, 10000); |
45
|
|
|
$this->client->send($request, $response); |
46
|
|
|
|
47
|
|
|
return $this->processResponse($response); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Response $response |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
private function processResponse(Response $response) |
56
|
|
|
{ |
57
|
|
|
$matches = []; |
58
|
|
|
|
59
|
|
|
preg_match('/<input.*value="(.*)"/U', $response->getContent(), $matches); |
60
|
|
|
|
61
|
|
|
if (!isset($matches[1])) { |
62
|
|
|
throw new \RuntimeException('Screenshot upload failed'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $matches[1]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param FormUpload $image |
70
|
|
|
* @param int $expire |
71
|
|
|
* @param string $auth |
72
|
|
|
* |
73
|
|
|
* @return FormRequest |
74
|
|
|
*/ |
75
|
|
|
private function buildRequest($image, $expire, $auth) |
76
|
|
|
{ |
77
|
|
|
$request = new FormRequest(); |
78
|
|
|
|
79
|
|
|
$request->fromUrl(self::REQUEST_URL); |
80
|
|
|
$request->setField('uploadedfile', $image); |
81
|
|
|
$request->setField('expire', $expire); |
82
|
|
|
$request->setField('upload', 1); |
83
|
|
|
$request->addHeader('Cookie: pie_auth=' . $auth); |
84
|
|
|
|
85
|
|
|
return $request; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|