Advertisement
jerusso

API Call example

Jun 20th, 2019
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. /*
  2. PHP methods that get and set product quantities for a class that makes calls to an API.
  3. */
  4.  
  5. public function getProductBalances($skus = '[]') {
  6.         //build header info. keys are added here
  7.         $headers = array();
  8.         $headers[] = 'Content-Type: application/json';
  9.         $headers[] = 'Ocp-Apim-Subscription-Key: ' . $this->developer_key;
  10.         $headers[] = 'ecd-subscription-key: ' . $this->account_integration_key;
  11.         $headers[] = 'Content-Length: ' . strlen($skus);
  12.        
  13.         $action = 'product/getProductBalances';//default action for this function
  14.        
  15.         $curl = curl_init();
  16.         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  17.         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  18.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  19.         curl_setopt($curl, CURLOPT_POSTFIELDS, $skus);
  20.         curl_setopt($curl, CURLOPT_URL, $this->url . $action);
  21.         $result = json_decode(curl_exec($curl) , true);
  22.         $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  23.  
  24.         if (curl_errno($curl)
  25.                 || $httpcode != 200
  26.                 || (!empty($result) && (isset($result['status']) && $result['status'] != 'Success'))
  27.                 ) {
  28.             $error_message = 'Error Processing Request: HTTP Code: ' . $httpcode . "\n" .
  29.                 var_export(curl_error($curl), true) . "\n" .
  30.                 var_export($result, 1);
  31.             curl_close($curl);
  32.             throw new Exception($error_message, 1);
  33.         }
  34.        
  35.         curl_close($curl);
  36.  
  37.         return $result;
  38.        
  39.        
  40.     }
  41.  
  42.     public function updateQtyOnHand ($skus = '[]') {
  43.        
  44.         //build header info. keys are added here
  45.         $headers = array();
  46.         $headers[] = 'Content-Type: application/json';
  47.         $headers[] = 'Ocp-Apim-Subscription-Key: ' . $this->developer_key;
  48.         $headers[] = 'ecd-subscription-key: ' . $this->account_integration_key;
  49.         $headers[] = 'Content-Length: ' . strlen($skus);
  50.        
  51.         $action = 'inventory/updateQuantityOnHand';//default action for this function
  52.        
  53.         $curl = curl_init();
  54.         curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
  55.         curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  56.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  57.         curl_setopt($curl, CURLOPT_POSTFIELDS, $skus);
  58.         curl_setopt($curl, CURLOPT_URL, $this->url . $action);
  59.         $result = json_decode(curl_exec($curl) , true);
  60.         $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  61.  
  62.         if (curl_errno($curl)
  63.                 || $httpcode != 200
  64.                 || (!empty($result) && (isset($result['status']) && $result['status'] != 'Success'))
  65.                 ) {
  66.             $error_message = 'Error Processing Request: HTTP Code: ' . $httpcode . "\n" .
  67.                 var_export(curl_error($curl), true) . "\n" .
  68.                 var_export($result, 1);
  69.             curl_close($curl);
  70.             throw new Exception($error_message, 1);
  71.         }
  72.        
  73.         curl_close($curl);
  74.  
  75.         return $result;
  76.        
  77.        
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement