-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BrowserClient implementation + tests
- Loading branch information
1 parent
534f9f4
commit 719bdfd
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Curl; | ||
|
||
class BrowserClient extends Client | ||
{ | ||
protected $headers = array( | ||
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||
'Accept-Encoding' => 'identity', | ||
'Accept-Language' => 'en-US,en;q=0.5', | ||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0' | ||
); | ||
|
||
protected $options = array( | ||
CURLOPT_ENCODING => '', // apparently curl will decode gzip automatically when this is empty | ||
CURLOPT_SSL_VERIFYHOST => 0, | ||
CURLOPT_SSL_VERIFYPEER => 0, | ||
CURLOPT_CONNECTTIMEOUT => 10, | ||
CURLOPT_TIMEOUT => 15 | ||
); | ||
|
||
protected $cookie_file; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$cookie_file = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), "BrowserClient"]); | ||
$this->setCookieFile($cookie_file); | ||
} | ||
|
||
public function setCookieFile($cookie_file) | ||
{ | ||
$this->cookie_file = $cookie_file; | ||
|
||
// read & write cookies | ||
$this->options[CURLOPT_COOKIEJAR] = $cookie_file; | ||
$this->options[CURLOPT_COOKIEFILE] = $cookie_file; | ||
} | ||
|
||
public function getCookieFile() | ||
{ | ||
return $this->cookie_file; | ||
} | ||
|
||
public function clearCookies() | ||
{ | ||
unlink($this->getCookieFile()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Curl\Tests; | ||
|
||
use Curl\BrowserClient; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BrowserTest extends TestCase | ||
{ | ||
public function test_cookies() | ||
{ | ||
$browser = new BrowserClient(); | ||
$browser->clearCookies(); | ||
|
||
$cookies = array( | ||
'cookie_one' => '111', | ||
'cookie_two' => 222 | ||
); | ||
|
||
$browser->get('https://httpbin.org/cookies/set', $cookies); | ||
|
||
$response = $browser->get('https://httpbin.org/cookies'); | ||
$json = json_decode($response->body, true); | ||
|
||
$this->assertEquals($cookies, $json['cookies']); | ||
} | ||
} |