PHP code example of ribal / onix

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

    

ribal / onix example snippets


$parser = new \Ribal\Onix\Parser();

/** @var \Ribal\Onix\Message\Message $message */
$message = $parser->parseString(
    file_get_contents('sample.xml')
);

/** @var \Ribal\Onix\Product\Product[] $products */
$products = $message->getProduct();

foreach ($products as $product) {
    $descriptiveDetail = $product->getDescriptiveDetail();
    $publishingDetail = $product->getPublishingDetail();
    // ...
}

use Ribal\Onix\CodeList\CodeList1;
use Ribal\Onix\CodeList\CodeList149;
use Ribal\Onix\CodeList\CodeList15;
use Ribal\Onix\CodeList\CodeList150;
use Ribal\Onix\CodeList\CodeList2;
use Ribal\Onix\CodeList\CodeList5;
use Ribal\Onix\Date;
use Ribal\Onix\Message\Header\Header;
use Ribal\Onix\Message\Header\Sender;
use Ribal\Onix\Message\Message;
use Ribal\Onix\Parser;
use Ribal\Onix\Product\DescriptiveDetail;
use Ribal\Onix\Product\Product;
use Ribal\Onix\Product\ProductIdentifier;
use Ribal\Onix\Product\TitleDetail;
use Ribal\Onix\Product\TitleElement;

$message = new Message();

$sender = new Sender();
$sender->setSenderName('Example Publishing Ltd.');
$header = new Header();
$header->setSender($sender);
$header->setSentDateTime(new Date(new \DateTime()));
$message->setHeader($header);

$product = new Product();
$product->setRecordReference('com.example.9781234567897');
$product->setNotificationType(CodeList1::resolve('03'));

$identifier = new ProductIdentifier();
$identifier->setProductIDType(CodeList5::resolve('15')); // ISBN-13
$identifier->setIDValue('9781234567897');
$product->addProductIdentifier($identifier);

$descriptiveDetail = new DescriptiveDetail();
$descriptiveDetail->setProductComposition(CodeList2::resolve('00'));
$descriptiveDetail->setProductForm(CodeList150::resolve('BC')); // Paperback

$titleElement = new TitleElement();
$titleElement->setTitleElementLevel(CodeList149::resolve('01'));
$titleElement->setTitleText('An Example Title');
$titleDetail = new TitleDetail();
$titleDetail->setTitleType(CodeList15::resolve('01'));
$titleDetail->addTitleElement($titleElement);
$descriptiveDetail->addTitleDetail($titleDetail);
$product->setDescriptiveDetail($descriptiveDetail);

$message->addProduct($product);

$parser = new Parser();
$xml = $parser->generate($message, 'reference', '3.1');

use Ribal\Onix\CodeList\CodeList198;
use Ribal\Onix\CodeList\CodeList91;
use Ribal\Onix\Product\ProductContact;

$contact = new ProductContact();
$contact->setProductContactRole(CodeList198::resolve('10')); // Product safety contact
$contact->setProductContactName('Example Publishing Ltd.');
$contact->setEmailAddress('[email protected]');
$contact->setStreetAddress('Example Street 1');
$contact->setLocationName('Berlin');
$contact->setPostalCode('10115');
$contact->setCountryCode(CodeList91::resolve('DE'));

$publishingDetail->addProductContact($contact);

$resource = new \Ribal\Onix\Product\SupportingResource();
$resource->setResourceContentType(CodeList158::resolve('01')); // Front cover
$resource->setContentAudience(CodeList154::resolve('00'));
$resource->setResourceMode(CodeList159::resolve('03'));        // Image
$resource->setResourceLink('https://example.com/cover.jpg');

// Either get the code list object and read its code and/or label
$type = $product->getNotificationType();
$code = $type->getCode();   // "03"
$value = $type->getValue(); // "Notification confirmed on publication"

// or directly get the label as string
$value = (string) $product->getNotificationType();

$productForm = \Ribal\Onix\CodeList\CodeList150::resolve('BC');

// Create parser using German code list labels
$parser = new \Ribal\Onix\Parser('de');

$descriptiveDetail = $product->getDescriptiveDetail();

foreach ($descriptiveDetail->getMeasure() as $measure) {
    echo sprintf('%s: %s %s',                      // -> "Height: 21 Centimeters"
        (string) $measure->getMeasureType(),       // e.g. "Height"
        $measure->getMeasurement(),                // e.g. "21"
        (string) $measure->getMeasureUnitCode()    // e.g. "Centimeters"
    );
}

/** @var \Ribal\Onix\Product\Measure $height */
$height = $descriptiveDetail->getHeight();
$width = $descriptiveDetail->getWidth();
$thickness = $descriptiveDetail->getThickness();
$weight = $descriptiveDetail->getWeight();

echo sprintf('Height: %s %s', $height->getMeasurement(), $height->getMeasureUnitCode()->getCode());
// --> Height: 21 cm
xml
<ONIXMessage>
    <Product>
        <NotificationType>03</NotificationType>
        [...]
    </Product>
</ONIXMessage>