Skip to content

Commit 719bdfd

Browse files
committed
BrowserClient implementation + tests
1 parent 534f9f4 commit 719bdfd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/BrowserClient.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Curl;
4+
5+
class BrowserClient extends Client
6+
{
7+
protected $headers = array(
8+
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
9+
'Accept-Encoding' => 'identity',
10+
'Accept-Language' => 'en-US,en;q=0.5',
11+
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'
12+
);
13+
14+
protected $options = array(
15+
CURLOPT_ENCODING => '', // apparently curl will decode gzip automatically when this is empty
16+
CURLOPT_SSL_VERIFYHOST => 0,
17+
CURLOPT_SSL_VERIFYPEER => 0,
18+
CURLOPT_CONNECTTIMEOUT => 10,
19+
CURLOPT_TIMEOUT => 15
20+
);
21+
22+
protected $cookie_file;
23+
24+
public function __construct()
25+
{
26+
parent::__construct();
27+
28+
$cookie_file = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), "BrowserClient"]);
29+
$this->setCookieFile($cookie_file);
30+
}
31+
32+
public function setCookieFile($cookie_file)
33+
{
34+
$this->cookie_file = $cookie_file;
35+
36+
// read & write cookies
37+
$this->options[CURLOPT_COOKIEJAR] = $cookie_file;
38+
$this->options[CURLOPT_COOKIEFILE] = $cookie_file;
39+
}
40+
41+
public function getCookieFile()
42+
{
43+
return $this->cookie_file;
44+
}
45+
46+
public function clearCookies()
47+
{
48+
unlink($this->getCookieFile());
49+
}
50+
}

tests/BrowserTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Curl\Tests;
4+
5+
use Curl\BrowserClient;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class BrowserTest extends TestCase
9+
{
10+
public function test_cookies()
11+
{
12+
$browser = new BrowserClient();
13+
$browser->clearCookies();
14+
15+
$cookies = array(
16+
'cookie_one' => '111',
17+
'cookie_two' => 222
18+
);
19+
20+
$browser->get('https://httpbin.org/cookies/set', $cookies);
21+
22+
$response = $browser->get('https://httpbin.org/cookies');
23+
$json = json_decode($response->body, true);
24+
25+
$this->assertEquals($cookies, $json['cookies']);
26+
}
27+
}

0 commit comments

Comments
 (0)