|
| 1 | +# A-Aparser PHP API |
| 2 | +This A-parser PHP API library provides convenient access to the [A-Parser HTTP API](https://en.a-parser.com/docs/api/overview). |
| 3 | + |
| 4 | +## Requirements |
| 5 | +- php >= 8.1 |
| 6 | +- guzzlehttp/guzzle >= 7.0 |
| 7 | + |
| 8 | +## Installation |
| 9 | +```shell |
| 10 | +composer require reset-button/a-parser-php-client |
| 11 | +``` |
| 12 | + |
| 13 | +## Quickstart |
| 14 | +```php |
| 15 | +//Define an a-parser instance |
| 16 | +$aparser = new \ResetButton\AparserPhpClient\Aparser('http://full_url_to_api_endpoint','password'); |
| 17 | + |
| 18 | +//Define an action from https://en.a-parser.com/docs/api/methods |
| 19 | +$taskConfAction = new \ResetButton\AparserPhpClient\Actions\GetTaskConfAction(1); |
| 20 | + |
| 21 | +//Run the action and get the result |
| 22 | +$result = $aparser->runAction($action); |
| 23 | +``` |
| 24 | +## Usage |
| 25 | +### Basic Action Usage |
| 26 | +Define an A-parser instance, passing full URL to API endpoint and password |
| 27 | +```php |
| 28 | +//Define an a-parser instance |
| 29 | +$aparser = new \ResetButton\AparserPhpClient\Aparser('full_url_to_api_endpoint','password'); |
| 30 | +``` |
| 31 | +Instantiate an action using the appropriate action class, as outlined in the official documentation |
| 32 | +```php |
| 33 | +$taskConfAction = new \ResetButton\AparserPhpClient\Actions\GetTaskConfAction(1); |
| 34 | +``` |
| 35 | +Run the action by calling `runAction` method |
| 36 | +```php |
| 37 | +$result = $aparser->runAction($action); |
| 38 | +``` |
| 39 | +### Using Parser class in Actions |
| 40 | + |
| 41 | +All actions that accept parser name and config as parameter, accept a `Parser` instance instead (addTask, oneRequest, bulkRequest, getParserPreset, getParserInfo) |
| 42 | + |
| 43 | +`Parser` class contains all logic, related to used parser in API methods, that used it |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | +In minimal approach you need only to create `Parser` instance without any configuration and pass it to corresponding method |
| 48 | + |
| 49 | +```php |
| 50 | +$parser = new \ResetButton\AparserPhpClient\Parser('HTML::EmailExtractor','parser preset, if differs from default'); |
| 51 | +$action = new \ResetButton\AparserPhpClient\Actions\GetParserInfoAction($parser); |
| 52 | +``` |
| 53 | + |
| 54 | +You can configure parser with options, overrides, etc before passing it to action by calling corresponding methods |
| 55 | +```php |
| 56 | +$parser->addOption('parseLevel', '1', ["limit" => 0]); |
| 57 | +$parser->addOverride('useproxy', false); |
| 58 | +``` |
| 59 | + |
| 60 | +Or you can set full configuration at once using a corresponding method, this is useful when copy pasting from API request |
| 61 | + |
| 62 | +<img src="./docs/setConfiguration.png" alt="drawing" height="500"/> |
| 63 | + |
| 64 | +```php |
| 65 | +$parser->setConfiguration([ |
| 66 | + [ |
| 67 | + "type" => "options", |
| 68 | + "id" => "parseLevel", |
| 69 | + "value" => 1, |
| 70 | + "additional" => [ |
| 71 | + "limit" => "0" |
| 72 | + ] |
| 73 | + ], |
| 74 | + [ |
| 75 | + "type" => "override", |
| 76 | + "id" => "useproxy", |
| 77 | + "value" => true |
| 78 | + ] |
| 79 | +]); |
| 80 | +``` |
| 81 | +> Note, that parser configuration has no effect in `getParserPreset` and `getParserInfo` actions. |
| 82 | +
|
| 83 | +### Configuring Actions |
| 84 | + |
| 85 | +All action required parameters are configured via constructor, and optional are set using builder pattern with corresponding parameters from the documentations with "set" prefix, for example |
| 86 | +```php |
| 87 | +//Example 1 |
| 88 | +$action = new \ResetButton\AparserPhpClient\Actions\GetProxiesAction(); |
| 89 | +$action->setCheckers(['premium']); //set checkers parameter |
| 90 | + |
| 91 | +//Example 2 |
| 92 | +$parser = new \ResetButton\AparserPhpClient\Parser('HTML::EmailExtractor','parser preset, if differs from default'); |
| 93 | +$action = new \ResetButton\AparserPhpClient\Actions\OneRequestAction($parser,'https://a-parser.com'); |
| 94 | +$action->setRawResults(true); |
| 95 | +$action->setDoLog(false); |
| 96 | +``` |
| 97 | +#### Configuring `addTask` action |
| 98 | +This method is differs from others methods, it can be instantiated via passing at least one parser or passing a [previously saved preset](https://a-parser.com/docs/api/methods#starting-a-previously-saved-task), this two methods are implemented via named constructor |
| 99 | + |
| 100 | +```php |
| 101 | +$parser = new \ResetButton\AparserPhpClient\Parser('HTML::EmailExtractor'); |
| 102 | +$actionViaParser = \ResetButton\AparserPhpClient\Actions\AddTaskAction::withParser($parser, ['query1']); |
| 103 | +$actionViaPreset = \ResetButton\AparserPhpClient\Actions\AddTaskAction::withPreset('savedPreset', ['query1']); |
| 104 | +``` |
| 105 | +Also, `addTask` action is a most complex action in API, so many setters for are not implemented and you should use `setDataValue` from Action Direct Methods to set parameters |
| 106 | +```php |
| 107 | +$actionViaPreset = \ResetButton\AparserPhpClient\Actions\AddTaskAction::withPreset('savedPreset', ['query1']); |
| 108 | +$actionViaPreset->setResultsUnique(); //Use setter |
| 109 | +$actionViaPreset->setDataValue("saveFailedQueries", true); //Setter for this option is missing, using direct method |
| 110 | +``` |
| 111 | +##### Setting queries source |
| 112 | +You should pass queries when creating action, no matter the source. And then change queries source using `setQueriesFrom...` methods, default behavior is from **text**. |
| 113 | + |
| 114 | +```php |
| 115 | +$fromText = \ResetButton\AparserPhpClient\Actions\AddTaskAction::withPreset('savedPreset', ['query1', 'query1']); |
| 116 | + |
| 117 | +$fromFile = \ResetButton\AparserPhpClient\Actions\AddTaskAction::withPreset('savedPreset', ['filename1.txt', 'filename2.txt']); |
| 118 | +$fromFile->setQueriesFromFiles(); |
| 119 | + |
| 120 | +//If you need change source again to text use |
| 121 | +// $fromFile->setQueriesFromText(); |
| 122 | +``` |
| 123 | + |
| 124 | +#### Action Direct Methods |
| 125 | + |
| 126 | +You can read any configuration option using `getDataValue` helper, if this option is not exists - null will returned |
| 127 | +```php |
| 128 | +$taskConfAction = new \ResetButton\AparserPhpClient\Actions\GetTaskConfAction(1); |
| 129 | +$taskConfAction->getDataValue("taskUid"); //will return 1 |
| 130 | +$taskConfAction->getDataValue("taskWrongUid"); //will return null |
| 131 | +``` |
| 132 | +You can directly set/overwrite any configuration option using `setDataValue` helper. Use with caution. |
| 133 | +```php |
| 134 | +$taskConfAction = new \ResetButton\AparserPhpClient\Actions\GetTaskConfAction(1); |
| 135 | +$taskConfAction->setDataValue("taskUid", 10); |
| 136 | +$taskConfAction->getDataValue("taskUid"); //will return 10 |
| 137 | +``` |
| 138 | +You can get all full configuration options using `getData` helper. |
| 139 | +```php |
| 140 | +$parser = new \ResetButton\AparserPhpClient\Parser("SE::Google") |
| 141 | +$parserPresetAction = new \ResetButton\AparserPhpClient\Actions\GetTaskConfAction($parser); |
| 142 | +$taskConfAction->getData(); //will return ["parser" => "SE::Google", "preset": "default"] |
| 143 | +``` |
| 144 | + |
| 145 | +### Aparser methods |
| 146 | + |
| 147 | +Instantiate A-parser instance by passing URL to API endpoint and password |
| 148 | +```php |
| 149 | +$aparser = new \ResetButton\AparserPhpClient\Aparser('http://full_url_to_api_endpoint','password'); |
| 150 | +``` |
| 151 | +Prepare actions and run it by calling `runAction` method |
| 152 | +```php |
| 153 | +$pingAction = new \ResetButton\AparserPhpClient\Actions\PingAction(); |
| 154 | +$getProxiesAction = new \ResetButton\AparserPhpClient\Actions\GetProxiesAction(); |
| 155 | +$resultPing = $aparser->runAction($pingAction); |
| 156 | +$resultGetProxies = $aparser->runAction($getProxiesAction); |
| 157 | +```` |
| 158 | +`runAction` will return `data` section from A-parser success response payload or throw a `AparserApiException` if A-parser responds with error |
| 159 | + |
| 160 | +You can get JSON string from passed action using `getJsonString` helper. |
| 161 | +```php |
| 162 | +$aparser->getJsonString($getPingAction); //will return something like {"password": "pass","action": "ping"} |
| 163 | +``` |
| 164 | + |
| 165 | +At last, you can just copy JSON query from A-parser and send it via API directly even ignoring previously passed password |
| 166 | +```php |
| 167 | +$jsonString = '{ "password": "pass", "action": "ping" }'; |
| 168 | +$aparser->runJsonString($jsonString); |
| 169 | +``` |
| 170 | + |
| 171 | +## License |
| 172 | + |
| 173 | +This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). |
0 commit comments