PHP code example of polyzon / polyzon-php

1. Go to this page and download the library: Download polyzon/polyzon-php 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/ */

    

polyzon / polyzon-php example snippets




nt = new Polyzon\Client(); // reads POLYZON_API_KEY

$page = $client->search('дракон', size: 10, free: true);
foreach ($page->items as $item) {
    echo "[{$item->source}] {$item->title} — {$item->url}\n";
}

$client = new Polyzon\Client(apiKey: $token);

use Polyzon\Lang;
use Polyzon\License;
use Polyzon\Sort;
use Polyzon\Source;

$page = $client->search(
    'органайзер для инструментов',      // ant | popular | newest | likes | makes | downloads
    free: true,                         // only free models
    sources: [Source::PRINTABLES, Source::THINGIVERSE],
    formats: ['stl', '3mf'],
    licenses: [License::CC0, License::CC_BY],
    lang: Lang::EN,                     // ru (default) | en — machine-translated titles and tags
);

$page->total;          // 1873
$page->totalRelation;  // 'eq' (exact) or 'gte' (lower bound)
$page->hasMore;        // is there a next page within the 2400-result window
$page->partial;        // part of the catalog did not answer
$page->correctedQuery; // the typo-fixed query the server searched for, or null
$page->items[0];       // SearchItem: source, id, title, url, thumbnail, creator, license, price, formats, tags, stats, publishedAt

foreach ($client->searchAll('shelf bracket', size: 50) as $i => $item) {
    echo $item->title, "\n";
    if ($i >= 119) {
        break; // 3 pages = 3 requests
    }
}

$model = $client->getModel('thingiverse', 'tv-1234567', lang: Lang::EN);
$model->description;
$model->images;  // URLs on the source's CDN
$model->license; // ModelLicense { id: 'cc-by-nc', raw: 'Creative Commons - Attribution - Non-Commercial' } or null
$model->copies;  // the same model on other sites: list of ModelRef { source, id }

$results = $client->getModels($page->items, concurrency: 4, lang: Lang::EN);
foreach ($results as $result) {
    if ($result->isOk()) {
        echo $result->model->title, "\n";
    } else {
        echo "{$result->ref->source}/{$result->ref->id}: {$result->error->getMessage()}\n";
    }
}

$me = $client->me();         // Me { account: { id, displayName, createdAt }, token: { id, name, prefix, createdAt } }
$limits = $client->limits(); // Limits { perMinute: { limit, used, remaining, resetAt }, daily: { … } }

$page = $client->search('ваза');
$page->meta->requestId; // X-Request-ID — quote it when contacting support
$page->meta->rateLimit; // RateLimit { limit: 200, remaining: 163, resetAt: 1791590400 } — the daily quota, or null

use Polyzon\Exception\GoneException;
use Polyzon\Exception\NotFoundException;
use Polyzon\Exception\QuotaExceededException;

try {
    $client->getModel('printables', 'pr-123456');
} catch (GoneException) {
    $db->delete('printables', 'pr-123456');
} catch (NotFoundException) {
    echo "no such model\n";
} catch (QuotaExceededException $e) {
    echo "quota is spent, retry in {$e->retryAfter} s\n";
}

$client = new Polyzon\Client(maxRetries: 5); // or 0 to disable

$client = new Polyzon\Client(timeout: 10.0);
$quick = $client->withOptions(timeout: 2.0, maxRetries: 0);

$client = new Polyzon\Client(proxy: 'http://user:[email protected]:3128');

$client = new Polyzon\Client(caBundle: '/etc/ssl/corporate-root-ca.pem');

// Same connections, different settings — e.g. one client per end-user token:
$forUser = $client->withOptions(apiKey: $userToken, maxRetries: 0);

$client = new Polyzon\Client(logger: function (string $level, string $message, array $context): void {
    error_log("[{$level}] {$message}");
});

// Any PSR-3 logger (Monolog, …):
$client = new Polyzon\Client(logger: $psrLogger->log(...));

$client = new Polyzon\Client(apiKey: 'test', baseUrl: 'http://127.0.0.1:8080', proxy: false);