Guest User

Untitled

a guest
Dec 12th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. class Api {
  2. private $authApi;
  3. private $logger;
  4.  
  5. function __construct()
  6. {
  7. global $_AUTH_API;
  8. $this->authApi = $_AUTH_API;
  9. $this->logger = new CustomLogs();
  10. }
  11.  
  12. public function getBrands()
  13. {
  14. $options = [
  15. 'request' => 'get_brands',
  16. ];
  17.  
  18. $brands = $this->requestApi($options);
  19.  
  20. return $brands->rows;
  21. }
  22.  
  23. public function get****() {}
  24.  
  25. /**
  26. * @param $data
  27. *
  28. * @return array|mixed|object
  29. * @throws Exception
  30. */
  31. private function requestApi($data)
  32. {
  33. $data = $this->authApi + $data;
  34.  
  35. $data = [
  36. 'data' => json_encode($data)
  37. ];
  38.  
  39. $ch = curl_init();
  40. curl_setopt($ch, CURLOPT_URL, "https://api.com/api/");
  41. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
  42. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);
  43. curl_setopt($ch, CURLOPT_POST, 1);
  44. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  46.  
  47. $answer = json_decode(curl_exec($ch));
  48. curl_close($ch);
  49.  
  50. if (empty($answer)) {
  51. $this->logger->log->error('Empty answer from API');
  52. throw new Exception('Empty answer from API');
  53. } else {
  54. if ($answer->success != true) {
  55. $this->logger->log->error('API error: ', (array) $answer );
  56. throw new Exception('API error: ' . $answer->message );
  57. } else {
  58. return $answer;
  59. }
  60. }
  61. }
  62. }
Add Comment
Please, Sign In to add comment