Skip to content

Instantly share code, notes, and snippets.

@devinsays
Last active February 7, 2021 15:11
Show Gist options
  • Save devinsays/dd15de71e3c563a3e6577acea7fa2d9d to your computer and use it in GitHub Desktop.
Save devinsays/dd15de71e3c563a3e6577acea7fa2d9d to your computer and use it in GitHub Desktop.
Create a JSON
<?php
/**
* Sets up the Easy Post API Endpoints
*
* @package WP Theming
*/
/**
* Sets up a JSON endpoint at /wp-json/easy-post/v1/rates/
*/
function easy_post_api_init() {
$namespace = 'easy-post/v1';
register_rest_route( $namespace, '/rates/', array(
'methods' => 'GET',
'callback' => 'easy_post_return_rates',
) );
}
add_action( 'rest_api_init', 'easy_post_api_init' );
/**
* Outputs Easy Post data on the JSON endpoint
*
* Defaults are used if query string data is missing, but you might want to return an error instead.
* Valid URL: /wp-json/easy-post/v1/rates/?zip=78701&width=10&length=10&height=10&weight=10&shipping_zip=78751
*/
function easy_post_return_rates( WP_REST_Request $request ) {
// Default shipping settings
$defaults = array(
'zip' => 78701,
'width' => 10,
'length' => 10,
'height' => 10,
'weight' => 16,
'shipping_zip' => 94105
);
// Get query strings params from request
$params = $request->get_query_params();
// Ensures all param keys are available
$params = array_replace( $defaults, $params );
// Sanitizes each of the keys
$params = array_map( "absint", $params );
// Creates unique key from values to set or retrieve transient
$key = 'easy_post_' . implode( "_", $params );
/**
* To avoid making the same request multiple times in a short time period
* we store the response as a transient in the WordPress database.
*
* This data will expire after 10 minutes.
*/
if ( false === ( $data = get_transient( $key ) ) ) {
$data = easy_post_make_request( $params );
$response = new WP_REST_Response( $data );
// Cache for 10 minutes
set_transient( $key, $response, 60 * 10 );
} else {
// Returns cached value
return get_transient( $key );
}
return $response;
}
/**
* Fetches data from the Easy Post API via wp_remote_post
*/
function easy_post_make_request( $params ) {
$to_address = array(
'zip' => $params['shipping_zip'],
'country' => 'US'
);
$from_address = array(
'zip' => $params['zip'],
'country' => 'US'
);
$parcel = array(
'length' => $params['length'],
'width' => $params['width'],
'height' => $params['height'],
'weight' => $params['weight']
);
$request = array(
'shipment' => array(
'to_address' => $to_address,
'from_address' => $from_address,
'parcel' => $parcel
)
);
$args = array(
'method' => 'POST',
'blocking' => true,
'headers' => array(
'Content-Type' => 'application/json'
),
'timeout' => 15,
'sslverify' => false,
'body' => json_encode( $request )
);
// Replace the EASYPOST_TEST_API_KEY constant with your own API Key
$api_url = 'https://' . EASYPOST_TEST_API_KEY . ':@api.easypost.com/v2/shipments';
$response = wp_remote_post( $api_url, $args );
if ( ! is_wp_error( $response ) ) {
$response = json_decode( $response['body'] );
}
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment