-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
user
committed
Jul 8, 2019
0 parents
commit c8da489
Showing
5 changed files
with
323 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,66 @@ | ||
<?php | ||
|
||
namespace ofilin\fssp; | ||
|
||
use yii\base\Component; | ||
use yii\base\InvalidConfigException; | ||
use yii\httpclient\Client; | ||
|
||
/** | ||
* @property \yii\httpclient\Client $client | ||
*/ | ||
class BaseClient extends Component | ||
{ | ||
const API_BASE_URL = 'https://api-ip.fssprus.ru/api/v1.0'; | ||
|
||
public $token; | ||
|
||
private $_client; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
if ($this->token === null) { | ||
throw new InvalidConfigException('Token property must be set'); | ||
} | ||
} | ||
|
||
/** | ||
* @return Client | ||
*/ | ||
public function getClient() | ||
{ | ||
if ($this->_client) { | ||
return $this->_client; | ||
} | ||
|
||
return new Client([ | ||
'baseUrl' => self::API_BASE_URL, | ||
'requestConfig' => [ | ||
'format' => Client::FORMAT_JSON | ||
], | ||
'responseConfig' => [ | ||
'format' => Client::FORMAT_JSON | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @param $method | ||
* @param array|null $data | ||
* @param string $http_method | ||
* @return mixed | ||
* @throws InvalidConfigException | ||
*/ | ||
public function call($method, array $data = null, $http_method = 'GET') | ||
{ | ||
$response = $this->getClient()->createRequest() | ||
->setMethod($http_method) | ||
->setUrl($method) | ||
->setData(array_merge($data, ['token' => $this->token])) | ||
->send(); | ||
|
||
return $response->data; | ||
} | ||
} |
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,121 @@ | ||
<?php | ||
|
||
namespace ofilin\fssp; | ||
|
||
use yii\base\ErrorException; | ||
|
||
class Client extends BaseClient | ||
{ | ||
public $retryTime = 1; // time between requests | ||
|
||
public $tryCount = 20; // max request count | ||
|
||
/** | ||
* @param array $data | ||
* @return mixed | ||
* @throws ErrorException | ||
* @throws FsspException | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function physical(array $data) | ||
{ | ||
$result = $this->fetch('/search/physical', $data); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return mixed | ||
* @throws ErrorException | ||
* @throws FsspException | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function legal(array $data) | ||
{ | ||
$result = $this->fetch('/search/legal', $data); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return mixed | ||
* @throws ErrorException | ||
* @throws FsspException | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function ip(array $data) | ||
{ | ||
$result = $this->fetch('/search/ip', $data); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return mixed | ||
* @throws ErrorException | ||
* @throws FsspException | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function group(array $data) | ||
{ | ||
$result = $this->fetch('/search/group', $data, true); | ||
return $result; | ||
} | ||
|
||
/** | ||
* @param $method | ||
* @param $data | ||
* @return mixed | ||
* @throws ErrorException | ||
* @throws FsspException | ||
* @throws \yii\base\InvalidConfigException | ||
*/ | ||
public function fetch($method, $data, $isPost = false) | ||
{ | ||
if ($isPost) { | ||
$response = $this->call($method, $data, 'POST'); | ||
} else { | ||
$response = $this->call($method, $data); | ||
} | ||
|
||
if (isset($response['status']) && $response['status'] == 'error') { | ||
throw new ErrorException($response['exception']); | ||
} | ||
|
||
if (!isset($response['response']['task'])) { | ||
var_dump($response); | ||
throw new ErrorException('Task id not received'); | ||
} | ||
$task = $response['response']['task']; | ||
|
||
$i = 0; | ||
do { | ||
$i++; | ||
sleep($this->retryTime); | ||
$resp_status = $this->call('/status', ['task' => $task]); | ||
$status = $resp_status["response"]["status"]; | ||
|
||
if ($i >= $this->tryCount) { | ||
throw new ErrorException('Timeout exceeded'); | ||
} | ||
} while ($status != 0); | ||
|
||
if ($resp_status['code'] != 0) { | ||
throw new ErrorException('Too many time'); | ||
} | ||
|
||
$response = $this->call('/result', ['task' => $task]); | ||
|
||
if (!isset($response["response"]["result"])) { | ||
throw new FsspException('Assertion error, unexpected result'); | ||
} | ||
|
||
if (count($response["response"]["result"]) == 1) { | ||
$response["response"]["result"]["0"]["result"]; | ||
} | ||
|
||
return $response["response"]["result"]; | ||
} | ||
|
||
|
||
} |
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,11 @@ | ||
<?php | ||
|
||
|
||
namespace ofilin\fssp; | ||
|
||
use yii\base\ErrorException; | ||
|
||
class FsspException extends ErrorException | ||
{ | ||
|
||
} |
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,99 @@ | ||
## yii2-fssp | ||
|
||
### Федеральная служба судебных приставов | ||
|
||
Интерфейс программирования приложений (API) БДИП ФССП России предназначен для получения сведений общедоступной части Банка данных исполнительных производств. | ||
|
||
Токен можно получить бесплатно здесь https://api-ip.fssprus.ru/register | ||
|
||
Компонент позволяет выполнять запросы: | ||
|
||
**Yii::$app->fssp->physical()** - запрос на поиск физического лица | ||
|
||
**Yii::$app->fssp->legal()** - запрос на поиск юридического лица | ||
|
||
**Yii::$app->fssp->ip()** - запрос на поиск по номеру исполнительного производства | ||
|
||
**Yii::$app->fssp->group()** - групповой запрос | ||
|
||
Получить подробную информацию можно здесь https://api-ip.fssprus.ru/swagger | ||
|
||
### Installation | ||
|
||
Add the package to your `composer.json`: | ||
|
||
{ | ||
"require": { | ||
"ofilin/yii2-fssp": "^0.1" | ||
} | ||
} | ||
|
||
and run `composer update` or alternatively run `composer require ofilin/yii2-fssp:^0.1` | ||
|
||
|
||
add to config file: | ||
``` | ||
'components' => [ | ||
// ... | ||
'fssp' => [ | ||
'class' => ofilin\fssp\Client::class, | ||
'token' => '__YOUR_TOKEN__', // Get free token from https://api-ip.fssprus.ru/ | ||
//'retryTime' => 1, // retry every 1 sec. | ||
//'tryCount' => 20, // 20 counts | ||
], | ||
// ... | ||
], | ||
``` | ||
### Usage | ||
``` | ||
<?php | ||
// Search by name | ||
$result = Yii::$app->fssp->physical([ | ||
'firstname' => 'Иванов', | ||
'secondname' => 'Иван', | ||
'lastname' => 'Иванович', | ||
'birthdate' => '10.10.1980', | ||
'region' => 78, | ||
]); | ||
// Search by company | ||
$result = Yii::$app->fssp->legal([ | ||
'region' => 78, | ||
'name' => 'ООО Рога и Копыта' | ||
]); | ||
// Search by number | ||
$result = Yii::$app->fssp->ip([ | ||
'number' => "1234/12/12345-ИП", | ||
]); | ||
// Group request returning many arrays!!! | ||
$result = Yii::$app->fssp->group([ | ||
'request' => [ | ||
[ | ||
'type' => 1, | ||
'params' => [ | ||
'firstname' => 'Иванов', | ||
'secondname' => 'Иван', | ||
'lastname' => 'Иванович', | ||
'birthdate' => '10.10.1980', | ||
'region' => 78, | ||
], | ||
], | ||
[ | ||
'type' => 2, | ||
'params' => [ | ||
'name' => 'ООО Рога и Копыта' | ||
'address' => 'Ленинская', | ||
'region' => 78, | ||
], | ||
], | ||
[ | ||
'type' => 3, | ||
'params' => [ | ||
'number' => "1234/12/12345-ИП", | ||
], | ||
], | ||
], | ||
]); | ||
``` |
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,26 @@ | ||
{ | ||
"name": "ofilin/yii2-fssp", | ||
"description": "FSSP component", | ||
"type": "yii2-extension", | ||
"keywords": [ | ||
"yii2", | ||
"fssp" | ||
], | ||
"license": "MIT", | ||
"minimum-stability": "dev", | ||
"authors": [ | ||
{ | ||
"name": "Oleg Filin", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "~2.0.0", | ||
"yiisoft/yii2-httpclient": "^2.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"ofilin\\fssp\\": "" | ||
} | ||
} | ||
} |