-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a note for future Net\Url::parse_str reimplementation. Added author to composer.json.
- Loading branch information
Showing
6 changed files
with
231 additions
and
22 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
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 |
---|---|---|
|
@@ -2,6 +2,12 @@ | |
"name": "anrdaemon/library", | ||
"description": "Published classes from private library", | ||
"license": "WTFPL", | ||
"authors": [ | ||
{ | ||
"name": "Andrey Repin", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"php": "^5.3.6 || ^7.0" | ||
}, | ||
|
@@ -13,7 +19,11 @@ | |
}, | ||
"autoload": { | ||
"psr-4": { | ||
"AnrDaemon\\": "src/", | ||
"AnrDaemon\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"AnrDaemon\\Tests\\": "test/" | ||
} | ||
}, | ||
|
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,20 @@ | ||
<?php | ||
/** Curl errors wrapper | ||
* | ||
* @version SVN: $Id: CurlException.php 788 2018-04-03 14:58:56Z anrdaemon $ | ||
*/ | ||
|
||
namespace AnrDaemon\Exceptions; | ||
|
||
class CurlException | ||
extends \RuntimeException | ||
{ | ||
public function __construct($curl = null, \Exception $previous = null) | ||
{ | ||
parent::__construct( | ||
is_resource($curl) ? curl_error($curl) : ($curl ?: 'Unable to initialize cURL instance - unknown error.'), | ||
is_resource($curl) ? curl_errno($curl) : 0, | ||
$previous | ||
); | ||
} | ||
} |
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,113 @@ | ||
<?php | ||
/** Simplistic curl wrapper with runtime cookie persistence | ||
* | ||
* @version SVN: $Id: Browser.php 788 2018-04-03 14:58:56Z anrdaemon $ | ||
*/ | ||
|
||
namespace AnrDaemon\Net; | ||
|
||
use | ||
AnrDaemon\Exceptions\CurlException; | ||
|
||
class Browser | ||
{ | ||
protected $curl; | ||
protected $info; | ||
|
||
protected function perform(callable $callback, ...$params) | ||
{ | ||
$result = $callback($this->curl, ...$params); | ||
if(curl_errno($this->curl) !== CURLE_OK) | ||
throw new CurlException($this->curl); | ||
|
||
if($result === false) | ||
throw new CurlException("Unable to perform $callback - unknown error."); | ||
|
||
return $result; | ||
} | ||
|
||
// Information and configuration | ||
|
||
public function getInfo($name) | ||
{ | ||
return $this->perform('curl_getinfo', $name); | ||
} | ||
|
||
public function setOpt($name, $value = null) | ||
{ | ||
return | ||
is_array($name) | ||
? $this->perform('curl_setopt_array', $name) | ||
: $this->perform('curl_setopt', $name, $value); | ||
} | ||
|
||
// Method handling | ||
|
||
public function get($url) | ||
{ | ||
$this->info = null; | ||
$this->setOpt([ | ||
CURLOPT_HTTPGET => true, | ||
CURLOPT_URL => "$url", | ||
]); | ||
$result = $this->perform('curl_exec'); | ||
$this->info = $this->perform('curl_getinfo'); | ||
return $result; | ||
} | ||
|
||
public function post($url, $data = null) | ||
{ | ||
$this->info = null; | ||
$this->setOpt([ | ||
CURLOPT_POST => true, | ||
CURLOPT_URL => "$url", | ||
CURLOPT_POSTFIELDS => $data ?: '', | ||
]); | ||
$result = $this->perform('curl_exec'); | ||
$this->info = $this->perform('curl_getinfo'); | ||
return $result; | ||
} | ||
/* // Does not work, requires an actual file resource. | ||
public function put($url, \SplFileObject $data) | ||
{ | ||
$this->setOpt([ | ||
CURLOPT_PUT => true, | ||
CURLOPT_INFILE => $data, | ||
]); | ||
return $this->perform('curl_exec'); | ||
} | ||
*/ | ||
/* | ||
public function customRequest($url, $data = null) | ||
{ | ||
$this->setOpt([ | ||
CURLOPT_CUSTOMREQUEST => '???', | ||
CURLOPT_URL => "$url", | ||
]); | ||
return $this->perform('curl_exec'); | ||
} | ||
*/ | ||
// Magic! | ||
|
||
public function __construct(array $params = null) | ||
{ | ||
$result = curl_init(); | ||
if($result === false) | ||
throw new CurlException; | ||
|
||
$this->curl = $result; | ||
$this->perform('curl_setopt_array', (array)$params + [ | ||
CURLOPT_COOKIEFILE => '', | ||
CURLOPT_COOKIESESSION => true, | ||
CURLOPT_SAFE_UPLOAD => true, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_CAINFO => ini_get('openssl.cafile'), | ||
CURLOPT_CAPATH => ini_get('openssl.capath'), | ||
]); | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
return $this->info[$name]; | ||
} | ||
} |
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
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