Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class FoxyCartApiClient
- {
- //App Specific
- private $app = "";
- private $config = "";
- public $access_token = "";
- //Vars
- public $last_status_code = "";
- //FoxyCart API Endpoints
- public $api_home_page = "https://api.foxycart.com";
- //public $api_home_page = "https://api-sandbox.foxycart.com";
- //Link Vars
- public $links = array();
- public $rel_base = "https://api.foxycart.com/rels/";
- public $registered_link_relations = array('self', 'first', 'prev', 'next', 'last');
- //Required Headers
- private $required_headers = array(
- 'FOXYCART-API-VERSION' => 1,
- );
- public function __construct($app, $access_token = "") {
- //So we can use these later
- $this->app = $app;
- //Set Access Token
- if ($access_token) {
- $this->access_token = $access_token;
- }
- }
- public function get($uri = "", $post = null) {
- return $this->go("GET", $uri, $post);
- }
- public function post($uri, $post = null) {
- return $this->go("POST", $uri, $post);
- }
- public function patch($uri, $post = null) {
- return $this->go("PATCH", $uri, $post);
- }
- public function delete($uri, $post = null) {
- return $this->go("DELETE", $uri, $post);
- }
- public function go($method, $uri, $post) {
- if (!is_array($post)) $post = null;
- if (!$uri) $uri = $this->api_home_page;
- $headers = $this->getHeaders();
- //PATCH Override
- if ($method == "PATCH") {
- $headers['X-HTTP-Method-Override'] = "PATCH";
- $method = "POST";
- }
- //GET Override
- if ($method == "GET" && $post != null) {
- if (strpos($uri, "?") === false) {
- $uri .= "?" . http_build_query($post);
- } else {
- $uri .= "&" . http_build_query($post);
- }
- $post = null;
- }
- //Send Request
- $api_request = $this->app['guzzle']->createRequest($method, $uri, $headers, $post)->send();
- $data = $api_request->json();
- $this->last_status_code = $api_request->getStatusCode();
- $this->saveLinks($data);
- return $data;
- }
- public function saveLinks($data) {
- if (!isset($data['_links'])) return;
- foreach ($data['_links'] as $key => $val) {
- $this->links[$key] = $val;
- }
- }
- public function getLink($str) {
- $search_string = in_array($str, $this->registered_link_relations) ? $str : $this->rel_base . $str;
- if (isset($this->links[$search_string])) {
- return $this->links[$search_string]['href'];
- } else {
- return "";
- }
- }
- public function checkForErrors($data) {
- if ($this->last_status_code != 200) {
- if (is_array($data) && isset($data['error_description'])) {
- return array($data['error_description']);
- } elseif (is_array($data) && isset($data[0]['message'])) {
- return array($data[0]['message']);
- } else {
- return array("No data returned.");
- }
- }
- return false;
- }
- public function getHeaders() {
- $headers = array_merge($this->required_headers, array(
- 'Accept' => 'application/hal+json',
- 'Content-Type' => 'application/json',
- ));
- if ($this->access_token) {
- $headers['Authorization'] = "Bearer " . $this->access_token;
- }
- return $headers;
- }
- //Set Fields on Init
- public function setApiHomepage($val) {
- $this->api_home_page = $val;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement