Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function adding_products() {
// API call
$feed = new Boats_API_Client( 'yachtall', 'en', array( 'code' => 'xxx', 'site_id' => 'xxx', 'bt_vers' => '3.0', 'api_vers' => '1.0' ) );
echo '<pre>';
var_dump($feed);
echo '</pre>';
}
<?php
/**
* WordPress JSON API client.
*
* @author Joseph VanTine
* @link https://dealersleague.com
*/
class Boats_API_Client {
/**
* Base path for all API user resources.
*
* @var string
*/
const YACHTALL_LIST = 'https://api.yachtall.com/en/boat-data';
/**
* Base URL for the WordPress site that the client is connecting to.
*
* @var string
*/
private $feed_name;
/**
* Language
*
* @var string
*/
private $lang;
/**
* API args
*
* @var string
*/
private $args;
/**
* Constructor.
*
* @param string $feed_name
* @param string $lang
* @param string $args
*/
public function __construct( $feed_name, $lang, $args = array() ) {
$this->feed_name = $feed_name;
$this->lang = $lang;
$this->args = $args;
}
/**
* Retrieve all of the boats.
*
* @param array $feed_name
* @param string $args
*
* @return array|WP_Error
*/
public function get_feed( $args = array() ) {
return $this->get( $this->build_url( $args ) );
}
/**
* Builds a full API request URL from the given endpoint URL and query string arguments.
*
* @param string $endpoint
* @param array $query
*
* @return string
*/
private function build_url( $args ) {
if ( $args['is_single'] == 'false' ) {
$is_single = '/boats/';
} else {
$is_single = '/boat/';
}
if ( $this->feed_name == 'yachtall' ) {
$url = self::YACHTALL_LIST.$is_single;
}
if ( !empty( $this->args ) ) {
$url .= '?'.rawurldecode(http_build_query( $this->args ));
}
if ( $args['is_single'] == 'true' && !empty( $args['id'] ) ) {
$url .= '&bid='.$args['id'];
}
return $url;
}
/**
* Converts the given response to a WP_Error object.
*
* @param array $response
*
* @return WP_Error
*/
private function convert_response_to_error(array $response)
{
$response = $this->decode_response($response);
$error = new WP_Error();
if ($response instanceof WP_Error) {
$error = $response;
} elseif (is_array($response)) {
array_walk($response, array($this, 'add_response_error'), $error);
}
return $error;
}
/**
* Decodes the JSON object returned in given response. Returns a WP_Error on error.
*
* @param array $response
*
* @return array|WP_Error
*/
private function decode_response(array $response)
{
$decoded = array();
$headers = $this->get_response_headers($response);
if (!isset($headers['content-type']) || false === stripos($headers['content-type'], 'application/json')) {
return new WP_Error('invalid_response', 'The content-type of the response needs to be "application/json".');
}
if (isset($response['body'])) {
$decoded = json_decode($response['body'], true);
}
if (null === $decoded) {
return new WP_Error('invalid_json', 'The JSON response couldn\'t be decoded.');
}
return $decoded;
}
/**
* Performs a GET request using the WordPress HTTP transport. Returns a WP_Error
* on error.
*
* @param string $url
* @param array $args
*
* @return array|WP_Error
*/
private function get( $response ) {
$response = wp_remote_request( $response );
if ( is_array( $response ) && $this->is_successful($response) ) {
$response = $this->decode_response($response);
} elseif ( !is_array( $response ) && !$this->is_successful($response) ) {
$response = $this->convert_response_to_error($response);
}
return $response;
}
/**
* Extracts the response headers from the given response.
*
* @param array $response
*
* @return array
*/
private function get_response_headers(array $response)
{
if (!isset($response['headers']) || !is_array($response['headers'])) {
$response = $response['headers'];
}
return $response;
}
/**
* Extracts the status code from the given response.
*
* @param array $response
*
* @return int|null
*/
private function get_response_status_code(array $response)
{
if (!isset($response['response']) || !isset($response['response']['code'])) {
return null;
}
return $response['response']['code'];
}
/**
* Checks if the given response is considered successful as per the HTTP specification.
* This means that the response has a 2xx status code.
*
* @param array $response
*
* @return bool
*/
private function is_successful(array $response)
{
$status_code = $this->get_response_status_code($response);
if (null === $status_code) {
return false;
}
return $status_code >= 200 && $status_code < 300;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment