PHP code example of xpiston / salsify

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

    

xpiston / salsify example snippets


use Piston\Salsify\Salsify;

$salsify = Salsify::with('your-api-token', 's-1234-5678-9012-3456');

$record = $salsify->records()->get('ABC-123');

$record->property('Brand');                       // 'Acme'
$record->localized('Description', 'fr-FR');       // 'Une description'
$record->version();                               // 7
$record->createdAt()?->format('Y-m-d');           // '2021-06-21'

echo $salsify->rateLimit();   // "9998 of 10000 requests left, resetting in 2410s"

// Only what moved. Salsify modifies only the properties you mention, so
// reading a record and writing it back whole would overwrite whatever
// somebody else changed in between.
$salsify->records()->update('ABC-123', ['Color' => 'Red', 'In Stock' => true]);

use Piston\Salsify\Entity\Record;

$salsify->records()->create(
    Record::of('SKU', 'ABC-124', ['Brand' => 'Acme', 'Color' => 'Blue'])
);

use Piston\Salsify\Exports\ExportConfiguration;

// 1. Ask. Returns immediately with an id.
$run = $salsify->exports()->start(ExportConfiguration::everything());

// 2. Come back later. Minutes to hours - from a scheduled job, not a web request.
$run = $salsify->exports()->status($run);

if (! $run->isReady()) {
    return;   // $run->progress() is 0 to 100
}

// 3. Read it, one line at a time, whatever its size.
foreach ($salsify->exports()->stream($run, compressed: true) as $line) {
    if ($line->isRecord()) {
        $record = $line->toRecord();
        // ...
    }
}

use Piston\Salsify\Exports\JsonLines;

foreach (JsonLines::records(JsonLines::open('/tmp/export.jsonl.gz', compressed: true)) as $record) {
    // One record in memory at a time.
}

use Piston\Salsify\Webhooks\InvalidSignature;
use Piston\Salsify\Webhooks\SignedRequest;

$verifier = $salsify->webhooks('https://shop.example.com/webhooks/salsify');

$webhook = SignedRequest::fromHeaders(getallheaders(), file_get_contents('php://input'));

try {
    $verifier->verify($webhook);
} catch (InvalidSignature $e) {
    http_response_code(403);
    error_log($e->getMessage());   // says which of six things was wrong
    exit;
}

// Laravel
use Piston\Salsify\Bridge\Laravel\WebhookRequest;

$webhook = WebhookRequest::from($request);   // uses $request->getContent()

// Symfony
use Piston\Salsify\Bridge\Symfony\WebhookRequest;

$webhook = WebhookRequest::from($request);

$limit = $salsify->rateLimit();

$limit->remaining;          // 9 412
$limit->isRunningOut();     // true below a twentieth - 500 of 10 000
$limit->isExhausted();
$limit->resetsIn;           // seconds until the top of the hour

use Piston\Salsify\Exception\RateLimited;

try {
    $salsify->records()->get('ABC-123');
} catch (RateLimited $e) {
    $retryAt = $e->resetsAt();   // a DateTimeImmutable, from the response
}

use Piston\Salsify\Exception\SalsifyException;   // the interface, if you want one catch
use Piston\Salsify\Exception\ApiError;           // status, body, messages()
use Piston\Salsify\Exception\Unauthorized;       // 401, 403
use Piston\Salsify\Exception\NotFound;           // 404
use Piston\Salsify\Exception\RateLimited;        // 429, with resetsAt()
use Piston\Salsify\Exception\TransportFailed;    // the network, or a non-JSON body
use Piston\Salsify\Exception\InvalidArgument;    // your mistake, before any request

use Piston\Salsify\Salsify;

final class ProductController
{
    public function show(Salsify $salsify, string $sku)
    {
        return $salsify->records()->get($sku)->toArray();
    }
}

use Piston\Salsify\Bridge\Laravel\WebhookRequest;
use Piston\Salsify\Webhooks\InvalidSignature;
use Piston\Salsify\Webhooks\SignatureVerifier;

Route::post('/webhooks/salsify', function (Request $request, SignatureVerifier $verifier) {
    try {
        $verifier->verify(WebhookRequest::from($request));
    } catch (InvalidSignature) {
        abort(403);
    }

    ProcessSalsifyWebhook::dispatch($request->json()->all());

    return response()->noContent();
});

// config/bundles.php
return [
    Piston\Salsify\Bridge\Symfony\SalsifyBundle::class => ['all' => true],
];

$run = $salsify->exports()->start(
    \Piston\Salsify\Exports\ExportConfiguration::everything()
);

var_dump($run->id(), $run->status(), $run->body);

// The row I would most like to hear about:
var_dump($salsify->exports()->status($run)->body);