PHP code example of christianjbrown / ebay-browse-api-sdk

1. Go to this page and download the library: Download christianjbrown/ebay-browse-api-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/ */

    

christianjbrown / ebay-browse-api-sdk example snippets


use ChristianBrown\EBay\Browse\BrowseFactory;
use ChristianBrown\EBay\Browse\Enums\MarketplaceId;
use ChristianBrown\EBay\Browse\Http\ApiHost;
use ChristianBrown\EBay\Browse\Marketplace;
use ChristianBrown\KeyValueStore\MemoryKeyValueStore;
use Symfony\Component\Clock\NativeClock;

$browse = (new BrowseFactory(ApiHost::production()))->create(
    'your-client-id',
    'your-client-secret',
    new Marketplace(MarketplaceId::EBAY_GB),   // optionally: end-user context, Accept-Language
    new MemoryKeyValueStore(new NativeClock())
);

$itemApi = $browse->getItemApi();                    // ItemApiInterface
$itemSummaryApi = $browse->getItemSummaryApi();      // ItemSummaryApiInterface
$compatibilityApi = $browse->getItemCompatibilityApi(); // ItemCompatibilityApiInterface

use ChristianBrown\EBay\Browse\BrowseFactory;
use ChristianBrown\EBay\Browse\Http\ApiHost;

$browse = (new BrowseFactory(ApiHost::sandbox()))->create(
    'your-sandbox-client-id',
    'your-sandbox-client-secret',
    new Marketplace(MarketplaceId::EBAY_GB),
    new MemoryKeyValueStore(new NativeClock())
);

$item = $itemApi->getOneByLegacyId('123456789012');   // ItemInterface

printf("%s — %s %s\n", $item->getTitle(), $item->getPrice()?->getValue(), $item->getPrice()?->getCurrency());
printf("Seller: %s (%s%% of %d)\n",
    $item->getSeller()?->getUsername(),
    $item->getSeller()?->getFeedbackPercentage(),
    $item->getSeller()?->getFeedbackScore()
);

// The quantity fields that replaced the retired Shopping API's QuantitySold.
foreach ($item->getEstimatedAvailabilities() as $availability) {
    printf("%s: %d sold, %d remaining\n",
        $availability->getEstimatedAvailabilityStatus(),   // IN_STOCK, OUT_OF_STOCK, …
        $availability->getEstimatedSoldQuantity(),
        $availability->getEstimatedRemainingQuantity()
    );
}

$group = $itemApi->getMultipleByItemGroupId('987654321098');   // ItemGroupInterface

foreach ($group->getItems() as $variation) {
    printf("%s — %s\n", $variation->getItemId(), $variation->getTitle());
}

$itemsResponse = $itemApi->getItems(['v1|123456789012|0', 'v1|234567890123|0']);   // ItemsResponseInterface

foreach ($itemsResponse->getItems() as $item) {
    printf("%s: %s %s\n", $item->getItemId(), $item->getPrice()?->getValue(), $item->getPrice()?->getCurrency());
}

printf("%d warnings\n", count($itemsResponse->getWarnings()));

$itemsResponse = $itemApi->getItems(itemGroupIds: ['987654321098']);

$collection = $browse->getItemSummaryApi()->search(
    q: 'vintage film camera',
    categoryIds: '15230',
    filter: 'buyingOptions:{FIXED_PRICE},price:[1..25],priceCurrency:GBP',
    sort: 'price',
    fieldgroups: 'MATCHING_ITEMS,ASPECT_REFINEMENTS',
    limit: 25,
    offset: 0
);

printf("%d matches, showing %d\n", $collection->getTotal(), count($collection->getItemSummaries()));

foreach ($collection->getItemSummaries() as $summary) {
    printf("%s — %s %s\n", $summary->getTitle(), $summary->getPrice()?->getValue(), $summary->getPrice()?->getCurrency());
}

// Refinements come back when you ask for them via fieldgroups.
foreach ($collection->getRefinement()?->getAspectDistributions() ?? [] as $aspect) {
    printf("%s\n", $aspect->getLocalizedAspectName());
    foreach ($aspect->getAspectValueDistributions() as $value) {
        printf("  %s (%d)\n", $value->getLocalizedAspectValue(), $value->getMatchCount());
    }
}

$collection = $browse->getItemSummaryApi()->searchByImage(
    base64_encode(file_get_contents('photo.jpg')),
    categoryIds: '15230',
    limit: 10
);

$response = $compatibilityApi->check('v1|236751498408|0', [
    'Car Make' => 'Honda',
    'Model' => 'Civic',
    'Engine' => '2.0',
]);

echo $response->getCompatibilityStatus(), "\n";   // COMPATIBLE, NOT_COMPATIBLE, UNDETERMINED

use ChristianBrown\EBay\Browse\Exception\ExceptionInterface;

try {
    $item = $itemApi->getOneByLegacyId('210987654321');
} catch (ExceptionInterface $exception) {
    // Anything this library throws lands here.
}

use ChristianBrown\EBay\Browse\Exception\ItemNotFoundException;

try {
    $item = $itemApi->getOneByLegacyId($legacyItemId);
} catch (ItemNotFoundException) {
    // The listing has ended — not an error worth retrying.
}

use ChristianBrown\ApiClient\ApiClientFactory;
use ChristianBrown\ApiClient\ClientOptions;
use ChristianBrown\EBay\Browse\Auth\Credentials;
use ChristianBrown\EBay\Browse\Enums\MarketplaceId;
use ChristianBrown\EBay\Browse\Http\ApiHost;
use ChristianBrown\EBay\Browse\Marketplace;
use ChristianBrown\KeyValueStore\MemoryKeyValueStore;
use ChristianBrown\OAuth2Client\ClientCredentialsTokenManagerFactory;
use ChristianBrown\OAuth2Client\Lock\NullLock;
use Symfony\Component\Clock\NativeClock;

// Shared JSON request sender (wires Guzzle for you).
$requestSender = (new ApiClientFactory(new ClientOptions()))->create()->getJsonApiRequestSender();

// ApiHost::production() talks to api.ebay.com; ApiHost::sandbox() switches
// every client and the token exchange below to eBay's sandbox gateway.
$apiHost = ApiHost::production();

// OAuth2 client-credentials machinery. The lock is 

use ChristianBrown\EBay\Browse\Api\ItemCompatibilityApi;
use ChristianBrown\EBay\Browse\Transformer\CompatibilityResponseTransformer;
use ChristianBrown\EBay\Browse\Transformer\ErrorParametersTransformer;
use ChristianBrown\EBay\Browse\Transformer\ErrorParameterTransformer;
use ChristianBrown\EBay\Browse\Transformer\ErrorsTransformer;
use ChristianBrown\EBay\Browse\Transformer\ErrorTransformer;
use ChristianBrown\EBay\Browse\Transformer\StringsTransformer;

$errorsTransformer = new ErrorsTransformer(
    new ErrorTransformer(
        new ErrorParametersTransformer(new ErrorParameterTransformer()),
        new StringsTransformer()
    )
);

$compatibilityApi = new ItemCompatibilityApi(
    $requestSender,
    new CompatibilityResponseTransformer($errorsTransformer),
    $credentials,
    $apiHost
);

use ChristianBrown\EBay\Browse\Api\ItemApi;
use ChristianBrown\EBay\Browse\Cache\ArrayKeyedCache;

$itemApi = new ItemApi(
    $requestSender,
    $itemTransformer,
    $itemGroupTransformer,
    $credentials,
    $apiHost,
    new ArrayKeyedCache(), // getOneById()
    new ArrayKeyedCache(), // getOneByLegacyId()
    new ArrayKeyedCache(), // getMultipleByItemGroupId()
    $itemsResponseTransformer,
    new ArrayKeyedCache()  // getItems()
);

// 1.x
$browse = new Browse($clientId, $clientSecret, $marketplace, $store);
$browse = new Browse($clientId, $clientSecret, $marketplace, $store, ApiHost::sandbox());

// 2.0
$browse = (new BrowseFactory(ApiHost::production()))->create($clientId, $clientSecret, $marketplace, $store);
$browse = (new BrowseFactory(ApiHost::sandbox()))->create($clientId, $clientSecret, $marketplace, $store);