SiteProApiClient.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. use ErrorException;
  3. class SiteProApiClient {
  4. /** @var string */
  5. protected $apiUrl;
  6. protected $apiUser;
  7. protected $apiPass;
  8. public function __construct($apiUrl, $apiUsername, $apiPassword) {
  9. $this->apiUrl = $apiUrl;
  10. $this->apiUser = $apiUsername;
  11. $this->apiPass = $apiPassword;
  12. }
  13. public function remoteCall($method, $params) {
  14. $url = $this->apiUrl.$method;
  15. $ch = curl_init();
  16. curl_setopt($ch, CURLOPT_URL, $url);
  17. curl_setopt($ch, CURLOPT_USERAGENT, 'Site.pro API Client/1.0.1 (PHP '.phpversion().')');
  18. curl_setopt($ch, CURLOPT_POST, true);
  19. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  20. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  21. 'Connection: Close',
  22. 'Content-Type: application/json',
  23. ));
  24. curl_setopt($ch, CURLOPT_TIMEOUT, 300);
  25. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  26. curl_setopt($ch, CURLOPT_USERPWD, $this->apiUser.':'.$this->apiPass);
  27. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  28. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Deaktiviert die Überprüfung des SSL-Zertifikats
  29. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Akzeptiert alle Hostnamen
  30. $r = curl_exec($ch);
  31. $errNo = curl_errno($ch);
  32. $errMsg = curl_error($ch);
  33. $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  34. curl_close($ch);
  35. if ($errNo) {
  36. $res = null;
  37. throw new ErrorException('Curl Error ('.$errNo.'): '.$errMsg);
  38. }
  39. if ($status != 200) {
  40. $res = json_decode($r);
  41. if (!$res) {
  42. $res = null;
  43. throw new ErrorException('Response Code ('.$status.')');
  44. }
  45. } else {
  46. $res = json_decode($r);
  47. }
  48. return $res;
  49. }
  50. }