Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace ShippingStation;
- class ShippingStation
- {
- /**
- * Shipping Station Curl
- * @var
- */
- private $curl;
- /**
- * Construct
- * @param $config
- */
- public function __construct($config)
- {
- $this->curl = new ShippingStationCurl($config['api_key'],$config['api_token']);
- }
- /**
- * Create a new ShipStation Account
- * @param $data
- */
- public function registerAccount($data)
- {
- $this->curl->post('accounts/registeraccount',$data);
- }
- /**
- * List all tags defined for this account
- */
- public function listTags()
- {
- $this->curl->get('accounts/listtags');
- }
- /**
- * Retrieves the shipping carrier account details for the specified carried code.
- * @param null $carrierCode
- */
- public function listCarriers($carrierCode = null)
- {
- $url = $carrierCode ? 'carriers/getcarrier?carrierCode='.$carrierCode : 'carriers/getcarrier';
- $this->curl->get($url);
- }
- /**
- * Add funds to a carrier account using payment information on the file
- * @param $data
- */
- public function addFunds($data)
- {
- $this->curl->post('carrier/addfunds',$data);
- }
- /**
- * Retrieves a list of packages for the specified carrier
- * @param null $carrierCode
- */
- public function listPackages($carrierCode = null)
- {
- $url = $carrierCode ? 'carriers/listpackages?carrierCode='.$carrierCode : 'carriers/listpackages';
- $this->curl->get($url);
- }
- /**
- * Retrieves a list of available shipping services for the specified carrier
- * @param null $carrierCode
- */
- public function listServices($carrierCode = null)
- {
- $url = $carrierCode ? 'carriers/listservices?carrierCode='.$carrierCode : 'carriers/listservices';
- $this->curl->get($url);
- }
- /**
- * Get a specific customer
- * @param null $customer
- */
- public function customer($customer = null)
- {
- $url = $customer ? "customers/{$customer}" : 'customers';
- $this->curl->get($url);
- }
- /**
- * params: stateCode, countryCode, tagId, marketplaceId, sortBy, sortDir, page, pageSize
- * @param array $params
- */
- public function customers($params = [])
- {
- $cust_params = '';
- if(!empty($params)) {
- foreach($params as $param => $value) {
- if($param == 'stateCode') {
- $cust_params .= "stateCode={$value}";
- }
- if($param == 'countryCode') {
- $cust_params .= "countryCode={$value}";
- }
- if($param == 'tagId') {
- $cust_params .= "tagId={$value}";
- }
- if($param == 'marketplaceId') {
- $cust_params .= "marketplaceId={$value}";
- }
- if($param == 'sortBy') {
- $cust_params .= "sortBy={$value}";
- }
- if($param == 'sortDir') {
- $cust_params .= "sortDir={$value}";
- }
- if($param == 'page') {
- $cust_params .= "page={$value}";
- }
- if($param == 'pageSize') {
- $cust_params .= "pageSize={$value}";
- }
- }
- }
- if($cust_params != '') {
- $this->curl->get("customers?{$cust_params}");
- }
- $this->curl->get('customers');
- }
- }
- <?php
- namespace ShippingStation;
- use Exception;
- use IlluminateContractsConfigRepository;
- class ShippingStationCurl
- {
- /**
- * Define method constants
- */
- const METHOD_POST = 'POST';
- const METHOD_PUT = 'PUT';
- const METHOD_GET = 'GET';
- const METHOD_DELETE = 'DELETE';
- /**
- * Api key
- * @var
- */
- protected $api_key;
- /**
- * Http code
- * @var
- */
- protected $http_code = 200;
- /**
- * Api secret
- * @var
- */
- protected $api_secret;
- /**
- * Error
- * @var string
- **/
- protected $errors = null;
- /**
- * Auth Token
- * @var
- */
- protected $token;
- /**
- * End point URL
- * @var string
- */
- private $endpoint = 'https://ss.shipstation.com/';
- /**
- * Array containing headers from last performed request.
- * @var array
- */
- private $headers = [
- 'Accept' => 'Accept: application/json'
- ];
- /**
- * @param string $key
- * @param string $value
- */
- public function setHeader($key, $value)
- {
- $this->headers[$key] = $value ? "{$key}: {$value}": $value;
- }
- /**
- * Constructor
- * @param Repository $config
- * @throws Exception
- */
- public function __construct($api_key, $api_secret)
- {
- /*if(empty($api_key) or empty($api_secret)) {
- throw new Exception('API Key/Secret is empty.');
- }*/
- $this->api_key = $api_key;
- $this->api_secret = $api_secret;
- // create tokens
- $this->token = base64_encode(trim($this->api_key . ':' . $this->api_secret));
- $this->setHeader('Authorization','Basic '. $this->token);
- }
- /**
- * Request method
- * @param $method
- * @param $url
- * @param array $parameters
- * @param array $headers
- * @return mixed
- * @throws Exception
- */
- private function request($method, $url, array $data = [], array $headers = [])
- {
- $curl = curl_init();
- curl_setopt_array($curl, [
- CURLOPT_URL => $this->endpoint . $url,
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_SSL_VERIFYPEER => 0,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_HEADER => 1,
- CURLINFO_HEADER_OUT => 1,
- CURLOPT_VERBOSE => 1,
- ]);
- switch ($method) {
- case 'PUT':
- case 'PATCH':
- case 'POST':
- curl_setopt_array($curl, [
- CURLOPT_CUSTOMREQUEST => $method,
- CURLOPT_POSTFIELDS => $data,
- ]);
- break;
- case 'DELETE':
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
- break;
- default:
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
- break;
- }
- curl_setopt($curl, CURLOPT_HTTPHEADER, array_filter(array_values($this->headers)));
- $response = curl_exec($curl);
- $this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- if(curl_error($curl)){
- $this->errors = curl_error($curl);
- }
- $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
- curl_close($curl);
- if(isset($this->errors)){
- throw new Exception($this->errors,$this->http_code);
- }
- $json = json_decode(substr($response, $header_size), false, 512, JSON_BIGINT_AS_STRING);
- return $json;
- }
- /**
- * GET
- * @param $url
- * @return mixed
- */
- public function get($url)
- {
- return $this->request(self::METHOD_GET, $url);
- }
- /**
- * POST
- * @param $url
- * @param array $data
- * @return mixed
- */
- public function post($url, $data = [])
- {
- return $this->request(self::METHOD_POST, $url, $data);
- }
- /**
- * PUT
- * @param $url
- * @param array $data
- * @return mixed
- */
- public function put($url, $data = [])
- {
- return $this->request(self::METHOD_PUT, $url, $data);
- }
- /**
- * DELETE
- * @param $url
- * @param array $data
- * @return mixed
- */
- public function delete($url, $data = [])
- {
- return $this->request(self::METHOD_DELETE, $url, $data);
- }
- }
Add Comment
Please, Sign In to add comment