Skip to content

Documentation

PHP SDK: Collecting JSON and using parameters

Some sites load products from a JSON API. Such a response is often easier to work with than HTML: prices are already numbers and fields have stable names. Inspect the page's network requests and check whether the response is available from the collection server.

Example source response

{"items":[{"id":"A-001","title":"Belt","price":1500.5}],"next":null}

Here next contains the next page URL or null. This is an illustrative source contract; replace field names with the real ones.

Example scenario

<?php
function run(\Sdk\Context $ctx): void
{
    $url = (string) $ctx->param('api_url', '');
    if ($url === '') {
        $ctx->abort('Set api_url');
    }
    $seen = [];
    $page = 0;
    while ($url !== '') {
        if (isset($seen[$url]) || $page >= 100) {
            $ctx->abort('Repeated page or page limit reached');
        }
        $seen[$url] = true;
        $response = $ctx->http()->withHeaders(['Accept' => 'application/json'])->get($url);
        if (!$response->ok()) {
            $ctx->abort('Source API returned HTTP ' . $response->status);
        }
        $data = $response->json();
        if (!isset($data['items']) || !is_array($data['items'])) {
            $ctx->abort('The response has no items list');
        }
        foreach ($data['items'] as $item) {
            if (!isset($item['id'])) {
                $ctx->log('Record skipped: no id', [], 'warning');
                continue;
            }
            $ctx->emit([
                'sku' => (string) $item['id'],
                'name' => $item['title'] ?? null,
                'price' => $item['price'] ?? null,
            ], (string) $item['id']);
        }
        $page++;
        $ctx->log('API page received', ['page' => $page, 'items' => count($data['items'])]);
        $next = $data['next'] ?? null;
        $url = is_string($next) && $next !== '' ? $ctx->url()->absolutize($response->finalUrl, $next) : '';
    }
}

If the source returns a cursor rather than a URL, pass it in the next request as the source requires. Do not replace a cursor with a page number.

Run parameters

$ctx->param('name', $default) reads one parameter; $ctx->params() returns the entire object. Pass the catalog address, test volume limit, and category selection as parameters so another run does not require a code change.

Never put passwords in log messages. Use $ctx->credential('name') for a configured source secret; the name must match the saved credential. Your Akmelo management API key is not the source site's API key.