Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.00 KB | None | 0 0
  1. <?php
  2.  
  3. /*  
  4.  *   FactuurSturen.nl, FactuurSturen.be, DigiRechnung.de API connector
  5.  *  
  6.  *   This class will help you connecting to our API. It's a little shell based around cURL.
  7.  *  
  8.  *   API Version: v1
  9.  *   Read our documentation for more help with this file:
  10.  *   https://www.factuursturen.nl/docs/api_v1.pdf
  11.  *  
  12.  *   There are several examples in the documentation to help you getting started.
  13.  *  
  14.  *   Happy coding and if you experience strange behaviour or if you're
  15.  *   stuck, drop us a line at support@factuursturen.nl
  16.  *  
  17.  */
  18.  
  19. class fsnl_api
  20. {
  21.     protected $url;
  22.     protected $verb;
  23.     protected $requestBody;
  24.     protected $requestLength;
  25.     protected $username;
  26.     protected $password;
  27.     protected $acceptType;
  28.     protected $responseBody;
  29.     protected $responseInfo;
  30.    
  31.     public function __construct ($url = null, $verb = 'GET', $requestBody = null)
  32.     {
  33.         $this->url              = $url;
  34.         $this->verb             = $verb;
  35.         $this->requestBody      = $requestBody;
  36.         $this->requestLength    = 0;
  37.         $this->username         = null;
  38.         $this->password         = null;
  39.         $this->acceptType       = 'application/json';
  40.         $this->responseBody     = null;
  41.         $this->responseInfo     = null;
  42.        
  43.         if ($this->requestBody !== null)
  44.         {
  45.             $this->buildPostBody();
  46.         }
  47.     }
  48.    
  49.     public function flush ()
  50.     {
  51.         $this->requestBody      = null;
  52.         $this->requestLength    = 0;
  53.         $this->verb             = 'GET';
  54.         $this->responseBody     = null;
  55.         $this->responseInfo     = null;
  56.     }
  57.    
  58.     public function execute ()
  59.     {
  60.         $ch = curl_init();
  61.         $this->setAuth($ch);
  62.        
  63.         try
  64.         {
  65.             switch (strtoupper($this->verb))
  66.             {
  67.                 case 'GET':
  68.                     $this->executeGet($ch);
  69.                     break;
  70.                 case 'POST':
  71.                     $this->executePost($ch);
  72.                     break;
  73.                 case 'PUT':
  74.                     $this->executePut($ch);
  75.                     break;
  76.                 case 'DELETE':
  77.                     $this->executeDelete($ch);
  78.                     break;
  79.                 default:
  80.                     throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
  81.             }
  82.         }
  83.         catch (InvalidArgumentException $e)
  84.         {
  85.             curl_close($ch);
  86.             throw $e;
  87.         }
  88.         catch (Exception $e)
  89.         {
  90.             curl_close($ch);
  91.             throw $e;
  92.         }
  93.        
  94.     }
  95.    
  96.     public function buildPostBody ($data = null)
  97.     {
  98.         $data = ($data !== null) ? $data : $this->requestBody;
  99.        
  100.         if (!is_array($data))
  101.         {
  102.             throw new InvalidArgumentException('Invalid data input for postBody.  Array expected');
  103.         }
  104.        
  105.         $data = http_build_query($data, '', '&');
  106.         $this->requestBody = $data;
  107.     }
  108.    
  109.     protected function executeGet ($ch)
  110.     {      
  111.         $this->doExecute($ch); 
  112.     }
  113.    
  114.     protected function executePost ($ch)
  115.     {
  116.         if (!is_string($this->requestBody))
  117.         {
  118.             $this->buildPostBody();
  119.         }
  120.        
  121.         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
  122.         curl_setopt($ch, CURLOPT_POST, 1);
  123.        
  124.         $this->doExecute($ch); 
  125.     }
  126.    
  127.     protected function executePut ($ch)
  128.     {
  129.         if (!is_string($this->requestBody))
  130.         {
  131.             $this->buildPostBody();
  132.         }
  133.        
  134.         $this->requestLength = strlen($this->requestBody);
  135.  
  136.         $fh = fopen('php://memory', 'rw');
  137.         fwrite($fh, $this->requestBody);
  138.         rewind($fh);
  139.        
  140.         curl_setopt($ch, CURLOPT_INFILE, $fh);
  141.         curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
  142.         curl_setopt($ch, CURLOPT_PUT, true);
  143.  
  144.         $this->doExecute($ch);
  145.        
  146.         fclose($fh);
  147.     }
  148.    
  149.     protected function executeDelete ($ch)
  150.     {
  151.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  152.        
  153.         $this->doExecute($ch);
  154.     }
  155.    
  156.     protected function doExecute (&$curlHandle)
  157.     {
  158.         $this->setCurlOpts($curlHandle);
  159.         $this->responseBody = curl_exec($curlHandle);
  160.         $this->responseInfo = curl_getinfo($curlHandle);
  161.        
  162.         curl_close($curlHandle);
  163.     }
  164.    
  165.     protected function setCurlOpts (&$curlHandle)
  166.     {
  167.         curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
  168.         curl_setopt($curlHandle, CURLOPT_URL, $this->url);
  169.         curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  170.         curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType));
  171.     }
  172.    
  173.     protected function setAuth (&$curlHandle)
  174.     {
  175.         if ($this->username !== null && $this->password !== null)
  176.         {
  177.             curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  178.             curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
  179.         }
  180.     }
  181.    
  182.     public function getAcceptType ()
  183.     {
  184.         return $this->acceptType;
  185.     }
  186.    
  187.     public function setAcceptType ($acceptType)
  188.     {
  189.         $this->acceptType = $acceptType;
  190.     }
  191.    
  192.     public function getPassword ()
  193.     {
  194.         return $this->password;
  195.     }
  196.    
  197.     public function setPassword ($password)
  198.     {
  199.         $this->password = $password;
  200.     }
  201.    
  202.     public function getResponseBody ()
  203.     {
  204.         return $this->responseBody;
  205.     }
  206.    
  207.     public function getResponseInfo ()
  208.     {
  209.         return $this->responseInfo;
  210.     }
  211.    
  212.     public function getUrl ()
  213.     {
  214.         return $this->url;
  215.     }
  216.    
  217.     public function setUrl ($url)
  218.     {
  219.         $this->url = $url;
  220.     }
  221.    
  222.     public function getUsername ()
  223.     {
  224.         return $this->username;
  225.     }
  226.    
  227.     public function setUsername ($username)
  228.     {
  229.         $this->username = $username;
  230.     }
  231.    
  232.     public function getVerb ()
  233.     {
  234.         return $this->verb;
  235.     }
  236.    
  237.     public function setVerb ($verb)
  238.     {
  239.         $this->verb = $verb;
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement