PHP code example of bim / bing-ads-sdk

1. Go to this page and download the library: Download bim/bing-ads-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bim / bing-ads-sdk example snippets


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);

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());
    }
}

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());

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'],
]);