Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1.     private function call($method, $url, $csrf = false, $data = false) {
  2.  
  3.         $ch = curl_init();
  4.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  5.         curl_setopt($ch, CURLOPT_URL, $url);
  6.         $headers = array();
  7.  
  8.         if ($data) {
  9.             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10.             $headers[] = 'Content-Type: application/json';
  11.             $headers[] = 'Content-Length: ' . strlen($data);
  12.         }
  13.         if ($csrf) {
  14.             $headers[] = 'X-XSRF-TOKEN: ' . $csrf;
  15.         }
  16.  
  17.         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  18.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  19.  
  20.         curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
  21.         curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiejar);
  22.  
  23.         return curl_exec($ch);
  24.     }
  25.  
  26.     private function csrf(){
  27.         return json_decode($this->call('POST', $this->api_url, false, "username={$this->api_username}&password={$this->api_password}"));
  28.     }
  29.  
  30.     function get($url) {
  31.         $response = $this->call('GET', $this->api_url . $url, $this->csrf());
  32.         return json_decode($response, true); //assoc array
  33. //        return $response; //json
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement