Advertisement
Guest User

Untitled

a guest
Oct 6th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. class Versio_API {
  3.    
  4.     function setApi_login($username, $password)
  5.     {
  6.         $this->loginusername = $username;
  7.         $this->loginpassword = $password;
  8.     }
  9.    
  10.     function setApi_debug()
  11.     {
  12.         $this->debug = true;
  13.     }
  14.  
  15.     function setApi_testmodus($testmodus)
  16.     {
  17.         if ($testmodus == 'true') {
  18.             $this->endpoint = 'https://www.versio.eu/testapi/v1';
  19.         }else{
  20.             $this->endpoint = 'https://www.versio.eu/api/v1';
  21.         }
  22.     }
  23.    
  24.         function setApi_output($outputresult)
  25.     {
  26.             $this->output = $outputresult;
  27.     }
  28.  
  29.     function request($requesttype, $request, $data=array())
  30.     {
  31.         $url = $this->endpoint.$request;
  32.         $ch = curl_init(); 
  33.         curl_setopt($ch, CURLOPT_USERPWD, $this->loginusername . ":" . $this->loginpassword);
  34.         curl_setopt($ch, CURLOPT_URL, $url);
  35.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $requesttype);
  36.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  37.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  38.         $result = curl_exec($ch);
  39.         $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  40.         curl_close($ch);
  41.        
  42.         if($this->debug) {
  43.             $debugdata = array('requesttype' => $requesttype, 'url' => $url, 'postdata' => $data, 'result' => $result, 'httpcode' => $httpcode);
  44.             var_dump($debugdata);
  45.         }
  46.        
  47.         if($this->output == 'json') {
  48.             $result = json_decode($result, 1);
  49.             $result['httpcode'] = $httpcode;
  50.            
  51.             return json_encode($result);
  52.            
  53.         } else {
  54.            
  55.             $result = json_decode($result, 1);
  56.             $result['httpcode'] = $httpcode;
  57.            
  58.             return $result;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement