PHP code example of ernadoo / mondial-relay

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

    

ernadoo / mondial-relay example snippets


use Ernadoo\MondialRelay\MondialRelayClient;

// Symfony's Psr18Client implements the PSR-18 client and both PSR-17 factories.
$psr18 = new \Symfony\Component\HttpClient\Psr18Client();

$client = MondialRelayClient::create(
    httpClient:     $psr18,
    requestFactory: $psr18,
    streamFactory:  $psr18,
    apiLogin:    'YOUR_API_LOGIN',
    apiPassword: 'YOUR_API_PASSWORD',
    brandCode:   'YOUR_BRAND_CODE',
    privateKey:  'YOUR_PRIVATE_KEY',
    sandbox:     false,
);

use Ernadoo\MondialRelay\Client\RestShipmentClient;
use Ernadoo\MondialRelay\Client\SoapParcelShopClient;
use Ernadoo\MondialRelay\MondialRelayClient;

$client = new MondialRelayClient(
    new RestShipmentClient($httpClient, $requestFactory, $streamFactory, 'API_LOGIN', 'API_PASSWORD', 'BRAND_CODE', sandbox: false),
    new SoapParcelShopClient('BRAND_CODE', 'PRIVATE_KEY'),
);

use Ernadoo\MondialRelay\Shipment\Address;
use Ernadoo\MondialRelay\Shipment\Parcel;
use Ernadoo\MondialRelay\Shipment\ShipmentRequest;

$request = new ShipmentRequest(
    sender: new Address(
        countryCode: 'FR',
        postCode:    '59510',
        city:        'Hem',
        streetName:  '4 Avenue Antoine Pinay',
        firstName:   'Erwan',
        lastName:    'Nader',
        mobileNo:    '+33600000000',
        email:       '[email protected]',
    ),
    recipient: new Address(
        countryCode: 'FR',
        postCode:    '75001',
        city:        'Paris',
        streetName:  '1 Rue de la Paix',
        firstName:   'Jane',
        lastName:    'Doe',
        mobileNo:    '+33600000001',
        email:       '[email protected]',
    ),
    parcels: [new Parcel(weightGrams: 500, content: 'Clothes')],
);

$response = $client->createShipment($request);

$response->shipmentNumber; // e.g. "12345678"
$response->labelOutput;    // URL of the PDF label
$response->trackingUrl;    // public tracking link

  $request = new ShipmentRequest(
      // ...
      deliveryLocation: 'FR-066974',
  );
  

$request = new ShipmentRequest(
    // ...
    deliveryMode: DeliveryMode::RELAY_XL, // 24L supports multi-parcel
    parcels: [
        new Parcel(weightGrams: 800, content: 'Shoes'),
        new Parcel(weightGrams: 600, content: 'Clothes'),
    ],
);

use Ernadoo\MondialRelay\ParcelShop\ParcelShopSearchRequest;
use Ernadoo\MondialRelay\Shipment\DeliveryMode;

$shops = $client->searchParcelShops(new ParcelShopSearchRequest(
    countryCode:      'FR',
    postCode:         '75001',
    deliveryMode:     DeliveryMode::RELAY, // only relay points compatible with 24R
    weightGrams:      500,
    searchDistanceKm: 10,
    maxResults:       7,
));

foreach ($shops as $shop) {
    $shop->name;           // "Tabac du Centre"
    $shop->distanceKm;     // 0.5
    $shop->locker;         // true for an automated parcel locker
    $shop->locationCode(); // "FR-066974": use it as deliveryLocation
}

$shops = $client->searchParcelShops(ParcelShopSearchRequest::around('FR', 47.9944, -4.0915, maxResults: 10));

$hours = $shop->schedule();
$hours->on(1);          // [['09:30', '13:00'], ['14:00', '19:00']]
$hours->on(7);          // [] — closed
$hours->isAlwaysOpen(); // true for a 24/7 locker; lockers inside a shop follow its opening hours

use Ernadoo\MondialRelay\Exception\ApiException;
use Ernadoo\MondialRelay\Exception\ConfigurationException;
use Ernadoo\MondialRelay\Exception\TransportException;

try {
    $response = $client->createShipment($request);
} catch (ApiException $e) {
    // Mondial Relay rejected the request (invalid phone number, unknown relay point…)
    foreach ($e->getErrors() as $code => $message) {
        echo "[$code] $message\n";
    }
} catch (TransportException $e) {
    // Mondial Relay unreachable, HTTP or SOAP error, malformed or incomplete response: retry later
} catch (ConfigurationException $e) {
    // A credential needed by the operation is missing
}

$client->setLogger($logger);