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