imper86 / php-allegro-api
Allegro.pl REST API 的 PHP SDK
v2.2.1
2023-10-08 09:58 UTC
Requires
- php: >=7.4
- ext-json: *
- imper86/http-client-builder: ^2.0
- imper86/php-http-cache-plugin: ^2.0
- lcobucci/jwt: ^4.0
Requires (Dev)
- http-interop/http-factory-guzzle: ^1.0
- imper86/vbump: ^1.0
- php-http/guzzle6-adapter: ^2.0
- phpstan/phpstan: ^0.12.86
- symfony/var-dumper: ^4.3
README
升级 v1.x.x -> v2.x.x
V2 引入了 PHP >=7.4 的支持(8.0 也受支持)。你只需更改 composer.json
文件中的版本。所有资源和方法都与 V1 相同,因此你无需更新你的代码。
V1 将不再维护,请尽快升级。
安装
只需使用 composer
composer require imper86/php-allegro-api
HTTPlug 注意事项
此库使用 HTTPlug,因此它不依赖于任何 HTTP 客户端。为了使用此库,你必须有一个 PSR-18 HTTP 客户端 和 PSR-17 HTTP 工厂。如果你不知道应该安装哪个,你可以要求这些
composer require php-http/guzzle6-adapter http-interop/http-factory-guzzle
身份验证和用法
库有一系列机制,允许你忘记令牌、过期等问题。但是,为了开始使用它,你必须使用 OAuth 流授权用户。
use Imper86\PhpAllegroApi\AllegroApi; use Imper86\PhpAllegroApi\Model\Credentials; use Imper86\PhpAllegroApi\Oauth\FileTokenRepository; use Imper86\PhpAllegroApi\Plugin\AuthenticationPlugin; // first, create Credentials object $credentials = new Credentials( 'your-client-id', 'your-client-secret', 'your-redirect-uri', true //is sandbox ); // create api client $api = new AllegroApi($credentials); // get the authorization URL, and redirect your user: $state = 'your-random-secret-state'; header(sprintf('Location: %s', $api->oauth()->getAuthorizationUri(true, $state))); /* * after successfull authorization, user will be refirected to your * redirect_uri with state and code as query parameters */ // verify the state and fetch token if ($state !== $_GET['state'] ?? null) { throw new Exception('CSRF?!'); } $token = $api->oauth()->fetchTokenWithCode($_GET['code']); // create TokenRepository object $tokenRepository = new FileTokenRepository( $token->getUserId(), __DIR__ . '/tokens' ); $tokenRepository->save($token); /* * You can invent your own TokenRepository, just implement * Imper86\PhpAllegroApi\Oauth\TokenRepositoryInterface * You can use your DB, Redis, or anything you want. */ // now you can add AuthenticationPlugin, which will take care // of maintaining your tokens $api->addPlugin(new AuthenticationPlugin($tokenRepository, $api->oauth())); // * note: of course you can use your own plugin, or AuthenticationPlugin from HTTPlug library // from now you can use these methods on AllegroApi object: $api->account()->(...); $api->afterSalesServiceConditions()->(...); $api->bidding()->(...); $api->billing()->(...); $api->me()->(...); $api->offers()->(...); $api->order()->(...); $api->payments()->(...); $api->pointsOfService()->(...); $api->pricing()->(...); $api->sale()->(...); $api->users()->(...); // fast example: var_dump($api->sale()->offers()->tags()->get('123456'));
如果你使用 PHPStorm 等具有类型提示的 IDE,你会很容易弄清楚。如果不,请查看 Resource 目录
设备流
use Imper86\PhpAllegroApi\AllegroApi; use Imper86\PhpAllegroApi\Model\Credentials; use Imper86\PhpAllegroApi\Oauth\FileTokenRepository; use Imper86\PhpAllegroApi\Plugin\AuthenticationPlugin; // first, create Credentials object $credentials = new Credentials( 'your-client-id', 'your-client-secret', 'your-redirect-uri', true //is sandbox ); // create api client $api = new AllegroApi($credentials); // Create authorization session $session = $api->oauth()->getDeviceCode(); // Provide device code and/or url to user echo 'Please visit: ' . $session->getVerificationUriComplete(); // Poll for authorization result $interval = $session->getInterval(); $token = false; do { sleep($interval); $device_code = $session->getDeviceCode(); try{ $token = $api->oauth()->fetchTokenWithDeviceCode($device_code); } catch (AuthorizationPendingException) { continue; } catch (SlowDownException) { $interval++; continue; } } while ($token == false); // create TokenRepository object $tokenRepository = new FileTokenRepository( $token->getUserId(), __DIR__ . '/tokens' ); $tokenRepository->save($token);
贡献
任何帮助都将非常感激 :)