PHP code example of easypost / easypost-php

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

    

easypost / easypost-php example snippets


# Require the autoloader (when using composer - recommended):
# will 



$client = new \EasyPost\EasyPostClient(getenv('EASYPOST_API_KEY'));

$shipment = $client->shipment->create([
    "from_address" => [
        "company" => "EasyPost",
        "street1" => "118 2nd Street",
        "street2" => "4th Floor",
        "city"    => "San Francisco",
        "state"   => "CA",
        "zip"     => "94105",
        "phone"   => "415-456-7890",
    ],
    "to_address" => [
        "name"    => "Dr. Steve Brule",
        "street1" => "179 N Harbor Dr",
        "city"    => "Redondo Beach",
        "state"   => "CA",
        "zip"     => "90277",
        "phone"   => "310-808-5243",
    ],
    "parcel" => [
        "length" => 20.2,
        "width"  => 10.9,
        "height" => 5,
        "weight" => 65.9,
    ],
]);

$boughtShipment = $client->shipment->buy($shipment->id, $shipment->lowestRate());

echo $boughtShipment;

function customFunction($args)
{
    // Pass your code here, data about the request/response is contained within `$args`.
    echo "Received a request with the status code of: " . $args['http_status'];
}

$client = new \EasyPost\EasyPostClient(getenv('EASYPOST_API_KEY'));

$client->subscribeToResponseHook('customFunction');

// Make your API calls here, your customFunction will trigger once a response is received

use EasyPost\Test\mocking\MockingUtility;
use EasyPost\Test\mocking\MockRequest;
use EasyPost\Test\mocking\MockRequestMatchRule;
use EasyPost\Test\mocking\MockRequestResponseInfo;

// create a mocking utility with a list of mock request-response pairs
$mockingUtility = new MockingUtility(
    new MockRequest(
        new MockRequestMatchRule(
            // HTTP method and regex pattern for the URL must both pass for the request to match
            'post',
            '/v2\\/bank_accounts\\/\\S*\\/charges$/'
        ),
        new MockRequestResponseInfo(
            // HTTP status code and body to return when this request is matched
            200,
            '{}'
        )
    ),
    ... // more mock requests
);

// create a new client with the mocking utility
$client = new EasyPostClient("some_key", Constants::TIMEOUT, Constants::API_BASE, $mockUtility);

// use the client as normal