Advertisement
sparkweb

FoxyCart REST API Guzzle Wrapper

Sep 9th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2. class FoxyCartApiClient
  3. {
  4.  
  5.     //App Specific
  6.     private $app = "";
  7.     private $config = "";
  8.     public $access_token = "";
  9.  
  10.     //Vars
  11.     public $last_status_code = "";
  12.  
  13.     //FoxyCart API Endpoints
  14.     public $api_home_page = "https://api.foxycart.com";
  15.     //public $api_home_page = "https://api-sandbox.foxycart.com";
  16.  
  17.     //Link Vars
  18.     public $links = array();
  19.     public $rel_base = "https://api.foxycart.com/rels/";
  20.     public $registered_link_relations = array('self', 'first', 'prev', 'next', 'last');
  21.  
  22.     //Required Headers
  23.     private $required_headers = array(
  24.         'FOXYCART-API-VERSION' => 1,
  25.     );
  26.  
  27.  
  28.     public function __construct($app, $access_token = "") {
  29.  
  30.         //So we can use these later
  31.         $this->app = $app;
  32.  
  33.         //Set Access Token
  34.         if ($access_token) {
  35.             $this->access_token = $access_token;
  36.         }
  37.  
  38.     }
  39.  
  40.  
  41.     public function get($uri = "", $post = null) {
  42.         return $this->go("GET", $uri, $post);
  43.     }
  44.  
  45.     public function post($uri, $post = null) {
  46.         return $this->go("POST", $uri, $post);
  47.     }
  48.  
  49.     public function patch($uri, $post = null) {
  50.         return $this->go("PATCH", $uri, $post);
  51.     }
  52.  
  53.     public function delete($uri, $post = null) {
  54.         return $this->go("DELETE", $uri, $post);
  55.     }
  56.  
  57.     public function go($method, $uri, $post) {
  58.         if (!is_array($post)) $post = null;
  59.         if (!$uri) $uri = $this->api_home_page;
  60.         $headers = $this->getHeaders();
  61.  
  62.         //PATCH Override
  63.         if ($method == "PATCH") {
  64.             $headers['X-HTTP-Method-Override'] = "PATCH";
  65.             $method = "POST";
  66.         }
  67.  
  68.         //GET Override
  69.         if ($method == "GET" && $post != null) {
  70.             if (strpos($uri, "?") === false) {
  71.                 $uri .= "?" . http_build_query($post);
  72.             } else {
  73.                 $uri .= "&" . http_build_query($post);
  74.             }
  75.             $post = null;
  76.         }
  77.  
  78.         //Send Request
  79.         $api_request = $this->app['guzzle']->createRequest($method, $uri, $headers, $post)->send();
  80.         $data = $api_request->json();
  81.         $this->last_status_code = $api_request->getStatusCode();
  82.         $this->saveLinks($data);
  83.         return $data;
  84.     }
  85.  
  86.  
  87.     public function saveLinks($data) {
  88.         if (!isset($data['_links'])) return;
  89.         foreach ($data['_links'] as $key => $val) {
  90.             $this->links[$key] = $val;
  91.         }
  92.     }
  93.     public function getLink($str) {
  94.         $search_string = in_array($str, $this->registered_link_relations) ? $str : $this->rel_base . $str;
  95.         if (isset($this->links[$search_string])) {
  96.             return $this->links[$search_string]['href'];
  97.         } else {
  98.             return "";
  99.         }
  100.     }
  101.  
  102.     public function checkForErrors($data) {
  103.         if ($this->last_status_code != 200) {
  104.             if (is_array($data) && isset($data['error_description'])) {
  105.                 return array($data['error_description']);
  106.             } elseif (is_array($data) && isset($data[0]['message'])) {
  107.                 return array($data[0]['message']);
  108.             } else {
  109.                 return array("No data returned.");
  110.             }
  111.         }
  112.         return false;
  113.     }
  114.  
  115.     public function getHeaders() {
  116.         $headers = array_merge($this->required_headers, array(
  117.             'Accept' => 'application/hal+json',
  118.             'Content-Type' => 'application/json',
  119.         ));
  120.         if ($this->access_token) {
  121.             $headers['Authorization'] = "Bearer " . $this->access_token;
  122.         }
  123.         return $headers;
  124.     }
  125.  
  126.     //Set Fields on Init
  127.     public function setApiHomepage($val) {
  128.         $this->api_home_page = $val;
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement