/src/MaxCDN.php

https://gitlab.com/Blueprint-Marketing/php-maxcdn · PHP · 116 lines · 62 code · 32 blank · 22 comment · 13 complexity · 821fcef835864d73eb9405bf315753f7 MD5 · raw file

  1. <?php
  2. /**
  3. * MaxCDN REST Client Library
  4. *
  5. * @copyright 2012
  6. * @author Karlo Espiritu
  7. * @version 1.0 2012-09-21
  8. */
  9. class MaxCDN {
  10. public $alias;
  11. public $key;
  12. public $secret;
  13. public $MaxCDNrws_url = 'https://rws.maxcdn.com';
  14. private $consumer;
  15. public function __construct($alias, $key, $secret, $options=null) {
  16. $this->alias = $alias;
  17. $this->key = $key;
  18. $this->secret = $secret;
  19. $this->consumer = new \MaxCDN\OAuth\OAuthConsumer($key, $secret, NULL);
  20. }
  21. private function execute($selected_call, $method_type, $params) {
  22. // the endpoint for your request
  23. $endpoint = "$this->MaxCDNrws_url/$this->alias$selected_call";
  24. //parse endpoint before creating OAuth request
  25. $parsed = parse_url($endpoint);
  26. if (array_key_exists("parsed", $parsed))
  27. {
  28. parse_str($parsed['query'], $params);
  29. }
  30. //generate a request from your consumer
  31. $req_req = \MaxCDN\OAuth\OAuthRequest::from_consumer_and_token($this->consumer, NULL, $method_type, $endpoint, $params);
  32. //sign your OAuth request using hmac_sha1
  33. $sig_method = new \MaxCDN\OAuth\OAuthSignatureMethod_HMAC_SHA1();
  34. $req_req->sign_request($sig_method, $this->consumer, NULL);
  35. // create curl resource
  36. $ch = curl_init();
  37. // set url
  38. curl_setopt($ch, CURLOPT_URL, $req_req);
  39. //return the transfer as a string
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  41. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , TRUE);
  42. // set curl timeout
  43. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  44. // set curl custom request type if not standard
  45. if ($method_type != "GET" && $method_type != "POST") {
  46. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_type);
  47. }
  48. if ($method_type == "POST" || $method_type == "PUT" || $method_type == "DELETE") {
  49. $query_str = \MaxCDN\OAuth\OAuthUtil::build_http_query($params);
  50. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:', 'Content-Length: ' . strlen($query_str)));
  51. curl_setopt($ch, CURLOPT_POSTFIELDS, $query_str);
  52. }
  53. // retrieve headers
  54. curl_setopt($ch, CURLOPT_HEADER, 1);
  55. curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
  56. //set user agent
  57. curl_setopt($ch, CURLOPT_USERAGENT, 'PHP MaxCDN API Client');
  58. // make call
  59. $result = curl_exec($ch);
  60. $headers = curl_getinfo($ch);
  61. $curl_error = curl_error($ch);
  62. // close curl resource to free up system resources
  63. curl_close($ch);
  64. // $json_output contains the output string
  65. $json_output = substr($result, $headers['header_size']);
  66. // catch errors
  67. if(!empty($curl_error) || empty($json_output)) {
  68. throw new \MaxCDN\RWSException("CURL ERROR: $curl_error, Output: $json_output", $headers['http_code'], null, $headers);
  69. }
  70. return $json_output;
  71. }
  72. public function get($selected_call, $params = array()){
  73. return $this->execute($selected_call, 'GET', $params);
  74. }
  75. public function post($selected_call, $params = array()){
  76. return $this->execute($selected_call, 'POST', $params);
  77. }
  78. public function put($selected_call, $params = array()){
  79. return $this->execute($selected_call, 'PUT', $params);
  80. }
  81. public function delete($selected_call, $params = array()){
  82. return $this->execute($selected_call, 'DELETE', $params);
  83. }
  84. }