Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: mfarid
  5.  * Date: 05/04/19
  6.  * Time: 22.54
  7.  */
  8.  
  9. namespace App\Adapter;
  10.  
  11.  
  12. /**
  13.  * Class Curl
  14.  * @package App\Adapter
  15.  */
  16. class Curl
  17. {
  18.     /**
  19.      * @param $signature
  20.      * @return array
  21.      */
  22.     private static function getHeaders($signature,$methodType){
  23.  
  24.         $headers = array();
  25.  
  26.         if($methodType=='get' || $methodType=='delete'){
  27.             $headers[] = 'Authorization:'.$signature;
  28.         }else{
  29.             $headers[] = 'Content-Type: application/json';
  30.             $headers[] = 'Authorization:'.$signature;
  31.  
  32.         }
  33.         return $headers;
  34.     }
  35.  
  36.     /**
  37.      * @param $url
  38.      * @param $signature
  39.      * @return mixed
  40.      */
  41.     public static function get($url, $signature)
  42.     {
  43.  
  44.         $ch = curl_init($url);
  45.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  46.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHeaders($signature,'get'));
  48.         $result = curl_exec($ch);
  49.         return json_decode($result);
  50.     }
  51.  
  52.     /**
  53.      * @param $url
  54.      * @param $postData
  55.      * @param $signature
  56.      * @return mixed
  57.      */
  58.     public static function post($url, $postData, $signature)
  59.     {
  60.  
  61.         $ch = curl_init($url);
  62.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  63.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  64.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  65.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  66.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHeaders($signature,'post'));
  67.  
  68.         $result = curl_exec($ch);
  69.         return json_decode($result);
  70.     }
  71.  
  72.     /**
  73.      * @param $url
  74.      * @param $postData
  75.      * @param $signature
  76.      * @return mixed
  77.      */
  78.     public static function put($url, $postData, $signature)
  79.     {
  80.         $headers = array();
  81.         $headers[] = 'Accept: application/json';
  82.         $headers[] = 'Content-Type: application/json';
  83.         $headers[] = 'Authorization:'.$signature;
  84.  
  85.         $ch = curl_init($url);
  86.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  87.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  88.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  89.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHeaders($signature,'put'));
  91.  
  92.         $result = curl_exec($ch);
  93.         return json_decode($result);
  94.     }
  95.  
  96.     /**
  97.      * @param $url
  98.      * @param $postData
  99.      * @param $signature
  100.      * @return mixed
  101.      */
  102.     public static function delete($url, $postData, $signature)
  103.     {
  104.  
  105.         $ch = curl_init($url);
  106.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
  107.         curl_setopt($ch, CURLOPT_FAILONERROR, true);
  108.         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
  109.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  110.         curl_setopt($ch, CURLOPT_HTTPHEADER, self::getHeaders($signature,'delete'));
  111.  
  112.         $result = curl_exec($ch);
  113.         return json_decode($result);
  114.     }
  115.  
  116.  
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement