bim / bing-ads-sdk
另一个Bing Ads SDK
v13.3.0
2022-05-14 05:17 UTC
Requires
- php: ^7.4
- ext-soap: *
- guzzlehttp/psr7: ^1.0|^2.0
- league/oauth2-client: ~2.0
- psr/event-dispatcher: ^1.0
- psr/http-message: ~1.0
Requires (Dev)
- phpunit/phpunit: ~7.2
- psr/log: ~1.0
- symfony/filesystem: ~4.1
- wsdl2phpgenerator/wsdl2phpgenerator: *
This package is not auto-updated.
Last update: 2024-09-28 19:47:22 UTC
README
这是官方Bing Ads PHP SDK的替代品。官方 Bing Ads PHP SDK。
它深受Google AdWords PHP SDK的启发,并有一些官方Bing SDK缺少的功能。
- 不需要
SoapVar
- 自动刷新访问令牌
它利用 league/oauth2-client
来处理oauth流程。
核心概念
- 会话 对象作为所有请求前配置(oauth凭证、开发者令牌、API环境等)的容器。
- 使用会话实例化一个 服务,并扩展
SoapClient
。服务用于向API发出实际请求。
使用示例
use PMG\BingAds\BingSession; use PMG\BingAds\BingServices; use PMG\BingAds\CustomerManagement as cm; $session = BingSession::builder() ->withEnvironment(BingSession::PROD) // or use BingSession::SANDBOX ->withOAuthClientId('changeme') ->withOAuthClientSecret('changeme') ->withOAuthRedirectUri('https://yourapp.com/oauth/microsoft') ->withRefreshToken('someRefreshToken') ->withDeveloperToken('someDeveloperToken') ->build() ; $services = new BingServices(); $service = $services->create(cm\CustomerManagementService::class, $session); $response = $service->getCustomersInfo(new cm\GetCustomersInfoRequest('a', 10)); print_r($response);
错误处理
如果发生soap错误,它将包装在 PMG\BingAds\Exception\ApiException
中。这个异常 可能 是特定于某个服务的,也可能是通用的 PMG\BingAds\Exception\SoapFault
。
要查看实际的HTTP请求和响应,请在soap客户端选项中将 trace
设置为true。
use PMG\BingAds\BingSession; use PMG\BingAds\BingServices; use PMG\BingAds\CustomerManagement as cm; $session = BingSession::builder() /* build a session as above */ ->build() ; $services = new BingServices(); $service = $services->create(cm\CustomerManagementService::class, $session, [ 'trace' => true, ]); try { $response = $service->getCustomersInfo(new cm\GetCustomersInfoRequest('a', 10)); } catch (cm\ApplicationFault $fault) { echo $fault->getTrackingId(), PHP_EOL; // a UUID to help the bing team debug /** @var Psr\Http\Message\RequestInterface $request */ $request = $fault->getRequest(); if ($request) { // request will be there if `trace => true` otherwise this will be null // show the SOAP XML request. The credentials here (access token, // developer token) will be redacted echo $request->getBody(), PHP_EOL; } /** @var Psr\Http\Message\ResponseInterface $response */ $response = $fault->getResponse(); if ($response) { // respone will be there if `trace => true` otherwise it's null // display the SOAP XML response echo $response->getBody(), PHP_EOL; } // show actual errors if ($fault instanceof cm\ApiFault) { print_r($fault->getOperationErrors()); } elseif ($fault instanceof cm\AdApiFault) { print_r($fault->getErrors()); } }
或者,每个服务都提供了一个 lastRequest
和 lastResponse
方法来查看HTTP请求/响应。
use PMG\BingAds\BingSession; use PMG\BingAds\BingServices; use PMG\BingAds\CustomerManagement as cm; $session = BingSession::builder() /* build a session as above */ ->build() ; $services = new BingServices(); $service = $services->create(cm\CustomerManagementService::class, $session, [ 'trace' => true, ]); $response = $service->getCustomersInfo(new cm\GetCustomersInfoRequest('a', 10)); print_r($service->lastRequest()); print_r($service->lastResponse());
身份验证
有关示例,请参阅 examples/auth。
实例化一个 PMG\BingAds\Auth\MicrosoftProvider
并像使用任何其他 oauth2-client
提供商 一样使用它。
use PMG\BingAds\Auth\MicrosoftProvider; $provider = MicrosoftProvider::production([ 'clientId' => 'changeme', 'clientSecret' => 'changeme', 'redirectUri' => 'https://youramp.com/oauth/microsft', ]); // get an authorization URL $authurl = $provider->getAuthorizationUrl(['prompt' => 'login']); // may want to $pvodier->getState() to save the state some place // exchange a `code` for an access token $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'], ]);