Advertisement
xenoside

Untitled

Jun 14th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2.  
  3. class CurlWrap
  4. {
  5.     private $ch;
  6.  
  7.     private function setOpts() {
  8.         curl_setopt_array($this->ch, [
  9.             CURLOPT_HEADER => 0,
  10.             CURLOPT_RETURNTRANSFER => true,
  11.             CURLOPT_CONNECTTIMEOUT => 10,
  12.             CURLOPT_TIMEOUT => 10
  13.         ]);
  14.     }
  15.  
  16.     public function __construct($url = null) {
  17.         $this->ch = curl_init($url);
  18.     }
  19.  
  20.     public function setUrl($url) {
  21.         curl_setopt($this->ch, CURLOPT_URL, $url);
  22.     }
  23.  
  24.     public function get() {
  25.         $this->setOpts();
  26.         $result = curl_exec($this->ch);
  27.         if(curl_errno($this->ch)) throw new Exception(curl_error($this->ch));
  28.         return $result;
  29.     }
  30.  
  31.     public function post($postData) {
  32.         curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postData);
  33.         return $this->get();
  34.     }
  35. }
  36.  
  37. class ConnectGido extends CurlWrap
  38. {
  39.     public function get() {
  40.         return json_decode(parent::get());
  41.     }
  42.  
  43.     public function post($postData) {
  44.         return parent::post(json_encode($postData));
  45.     }
  46.  
  47.     public function login($phone, $password) {
  48.         $this->setUrl('https://test-api.vn/account/login');
  49.         return $this->post(['phone' => $phone, 'password' => $password]);
  50.     }
  51. }
  52.  
  53.  
  54. $gido = new ConnectGido();
  55. $result = $gido->login('09843211', '123456');
  56. print_r($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement