Added basic authentication
This commit is contained in:
parent
d69ce96a64
commit
92251b4b14
@ -34,6 +34,12 @@ Or you can add this line in composer.json
|
|||||||
use Web3\Web3;
|
use Web3\Web3;
|
||||||
|
|
||||||
$web3 = new Web3('http://localhost:8545');
|
$web3 = new Web3('http://localhost:8545');
|
||||||
|
|
||||||
|
// timeout
|
||||||
|
$web3 = new Web3('http://localhost:8545', 0.1);
|
||||||
|
|
||||||
|
// basic auth
|
||||||
|
$web3 = new Web3('http://localhost:8545', 0.1, "AuthUser", "AuthPassword");
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using provider
|
### Using provider
|
||||||
|
@ -36,9 +36,9 @@ class HttpRequestManager extends RequestManager implements IRequestManager
|
|||||||
* @param int $timeout
|
* @param int $timeout
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($host, $timeout = 1)
|
public function __construct($host, $timeout = 1, $authu = NULL, $authp = NULL)
|
||||||
{
|
{
|
||||||
parent::__construct($host, $timeout);
|
parent::__construct($host, $timeout, $authu, $authp);
|
||||||
$this->client = new Client;
|
$this->client = new Client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,10 +56,20 @@ class HttpRequestManager extends RequestManager implements IRequestManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$res = $this->client->post($this->host, [
|
|
||||||
'headers' => [
|
if(!$this->authu):
|
||||||
|
$headers = [
|
||||||
'content-type' => 'application/json'
|
'content-type' => 'application/json'
|
||||||
],
|
];
|
||||||
|
else:
|
||||||
|
$headers = [
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'Authorization' => 'Basic ' . base64_encode($this->authu . ":" . $this->authp)
|
||||||
|
];
|
||||||
|
endif;
|
||||||
|
|
||||||
|
$res = $this->client->post($this->host, [
|
||||||
|
'headers' => $headers,
|
||||||
'body' => $payload,
|
'body' => $payload,
|
||||||
'timeout' => $this->timeout,
|
'timeout' => $this->timeout,
|
||||||
'connect_timeout' => $this->timeout
|
'connect_timeout' => $this->timeout
|
||||||
|
@ -27,17 +27,35 @@ class RequestManager
|
|||||||
*/
|
*/
|
||||||
protected $timeout;
|
protected $timeout;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* auth username
|
||||||
|
*
|
||||||
|
* @var float
|
||||||
|
*/
|
||||||
|
protected $authu;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* auth password
|
||||||
|
*
|
||||||
|
* @var float
|
||||||
|
*/
|
||||||
|
protected $authp;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* construct
|
* construct
|
||||||
*
|
*
|
||||||
* @param string $host
|
* @param string $host
|
||||||
* @param float $timeout
|
* @param float $timeout
|
||||||
|
* @param string $authu
|
||||||
|
* @param string $authp
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($host, $timeout=1)
|
public function __construct($host, $timeout = 1, $authu = NULL, $authp = NULL)
|
||||||
{
|
{
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->timeout = (float) $timeout;
|
$this->timeout = (float) $timeout;
|
||||||
|
$this->authu = $authu;
|
||||||
|
$this->authp = $authp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,12 +87,12 @@ class Web3
|
|||||||
* @param string|\Web3\Providers\Provider $provider
|
* @param string|\Web3\Providers\Provider $provider
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct($provider)
|
public function __construct($provider, $timeout = 1, $authu = NULL, $authp = NULL)
|
||||||
{
|
{
|
||||||
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
|
||||||
// check the uri schema
|
// check the uri schema
|
||||||
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
if (preg_match('/^https?:\/\//', $provider) === 1) {
|
||||||
$requestManager = new HttpRequestManager($provider);
|
$requestManager = new HttpRequestManager($provider, $timeout, $authu, $authp);
|
||||||
|
|
||||||
$this->provider = new HttpProvider($requestManager);
|
$this->provider = new HttpProvider($requestManager);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user