multoo/shipping-myparcel

v3.2.6 2024-03-26 11:46 UTC

README

API documentatie: http://www.myparcel.nl/api-documentatie

/**
 * Communication with the MyParcel API
 */

session_start();

define('TARGET_SITE_API', 'http://www.myparcel.nl/api/');

define('NONCE',     0); // note: this should be incremented in case 2 requests occur within the same timestamp (second)
define('TIMESTAMP', time());
define('USERNAME',  ''); // your username
define('API_KEY',   ''); // your key is provided by MyParcel

/**
 * Authentication example
 */
if(isset($_GET['example']) && $_GET['example'] == 'auth')
{
    // create JSON
    $array = array(
        'test JSON' => '1337',
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $get = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'timestamp=' . TIMESTAMP,
        'username=' . USERNAME,
    ));

    // create hash
    $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($get), API_KEY);

    // create final request
    $request = TARGET_SITE_API . 'test-authentication/?' . $get . '&signature=' . $signature;

    // process request
    $result = file_get_contents($request);

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);

    exit;
}

/**
 * Consignment example
 */
if(isset($_GET['example']) && $_GET['example'] == 'consignment')
{
    // NOTE: You can create multiple consignments by creating an array of multiple JSON arrays like below.
    //       If you do, please make sure you call the create-consignments action (with an 's' at the end).

    // create JSON
    $array = array(
        'process'     => 0, // NOTE: process parameter is inactive, put on 1 to create a consignment and process it
        'consignment' => array(
            'shipment_type'  => 'standard', // standard | letterbox | unpaid_letter
            'ToAddress'      => array(
                'country_code'    => 'NL', // letterbox packages will always be forced to NL
                'name'            => 'Lars',
                'business'        => 'MyParcel',
                'postcode'        => '2131BC', // if country_code = NL
                'house_number'    => '679',    // if country_code = NL
                'number_addition' => '',       // if country_code = NL
                'eps_postcode'    => '',       // if country_code != NL
                'street'          => 'Hoofdweg',
                'town'            => 'Hoofddorp',
                'email'           => 'info@myparcel.nl',
                'phone_number'    => '',
            ),
            'PgAddress'      => array( // NOTE: only include if you are shipping to a PostNL PakjeGemak location
                'name'            => '', // should contain an address exactly as returned by the Locatiekiezer
                'street'          => '',
                'house_number'    => '',
                'number_addition' => '',
                'postcode'        => '',
                'town'            => '',
            ),
            'ProductCode'    => array(
                'extra_size'           => 0,
                'home_address_only'    => 1,
                'signature_on_receipt' => 1,
                'return_if_no_answer'  => 0,
                'insured'              => 0,
            ),
            'insured_amount' => '',
            'custom_id'      => 'TEST API',
            'comments'       => '',
        ),
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $string = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'test=' . 1, // NOTE: test parameter is active, put on 0 to actually create a consignment
        'timestamp=' . TIMESTAMP,
        'username=' . USERNAME,
    ));

    $method = 'POST'; // GET or POST

    if($method == 'POST')
    {
        // hash request POST example
        $signature = hash_hmac('sha1', 'POST' . '&' . urlencode($string), API_KEY);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, TARGET_SITE_API . 'create-consignment');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $string . '&signature=' . $signature);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close ($ch);
    }
    else // GET
    {
        // hash request GET example
        $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($string), API_KEY);
        $request = TARGET_SITE_API . 'create-consignment/?' . $string . '&signature=' . $signature;
        $result = file_get_contents($request);
    }

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);
    exit; // remove the exit to continue into the print function, which will process the consignment

    /**
     * PDF example (this is another api call) NOTE: this will process the consignment, if not already processed
     */
    // create JSON
    $array = array(
        'consignment_id' => $decode['consignment_id'],
        'format'         => 'json',
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $string = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'timestamp=' . time(),
        'username=' . USERNAME,
    ));

    // create hash
    $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($string), API_KEY);

    // create final request
    $request = TARGET_SITE_API . 'retrieve-pdf/?' . $string . '&signature=' . $signature;

    // display pdf link (for this example to work, you should use format=pdf in the JSON)
    echo '<a href="' . $request . '" target="_blank">' . $request . '</a>';

    // process request
    $result = file_get_contents($request);

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);

    exit;
}

/**
 * Status example
 */
if(isset($_GET['example']) && $_GET['example'] == 'status')
{
    // create JSON
    $array = array(
        'consignment_id' => '2596171', // your consignment ID here (a comma-separated list is also possible)
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $get = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'timestamp=' . TIMESTAMP,
        'username=' . USERNAME,
    ));

    // create hash
    $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($get), API_KEY);

    // create final request
    $request = TARGET_SITE_API . 'retrieve-status/?' . $get . '&signature=' . $signature;

    echo $request;

    // process request
    $result = file_get_contents($request);

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);

    exit;
}

/**
 * Retourlink example
 */
if(isset($_GET['example']) && $_GET['example'] == 'retourlink')
{
    // create JSON
    $array = array(
        'email' => 'martin.boer@trq.nl', // no data | 'email' | 'consignment_id'
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $get = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'timestamp=' . TIMESTAMP,
        'username=' . USERNAME,
    ));

    // create hash
    $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($get), API_KEY);

    // create final request
    $request = TARGET_SITE_API . 'create-retourlink/?' . $get . '&signature=' . $signature;

    // process request
    $result = file_get_contents($request);

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);

    exit;
}

/**
 * Credit consignment example
 */
if(isset($_GET['example']) && $_GET['example'] == 'credit')
{
    // create JSON
    $array = array(
        'consignment_id' => '12345',
    );
    $json = urlencode(json_encode($array));

    // create GET string (keys in alphabetical order)
    $get = implode('&', array(
        'json=' . $json,
        'nonce=' . NONCE,
        'timestamp=' . TIMESTAMP,
        'username=' . USERNAME,
    ));

    // create hash
    $signature = hash_hmac('sha1', 'GET' . '&' . urlencode($get), API_KEY);

    // create final request
    $request = TARGET_SITE_API . 'consignment-credit/?' . $get . '&signature=' . $signature;

    // process request
    $result = file_get_contents($request);

    // show result
    $decode = json_decode($result, true);
    var_dump($decode);

    exit;
}