Skip to content

Instantly share code, notes, and snippets.

@hololoev
Last active March 20, 2018 13:41
Show Gist options
  • Save hololoev/f24144f97fafd9d5b6f60a3ff3ed7efc to your computer and use it in GitHub Desktop.
Save hololoev/f24144f97fafd9d5b6f60a3ff3ed7efc to your computer and use it in GitHub Desktop.
class CurlRequest
{
public $api_url = "";
public $key = "";
function __construct($key, $api_url, $timeOut)
{
$this->key = $key;
$this->api_url = $api_url;
$this->timeOut = $timeOut;
}
public function getTocken($url, $rType) {
return md5($this->key . $url . $rType);
}
public function get($path, $params=[]) {
$query = '';
foreach($params as $key => $val) {
$query .= "$key=$val&";
}
$url = $this->api_url . $path . '?' . query;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['auth-token: ' . $this->getTocken($path, 'get') ]);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->timeOut);
try {
$curlResponse = curl_exec($curl);
} catch(err) {
return null;
}
if(curl_exec($curl) === false) {
return null;
}
curl_close($curl);
return json_decode($curlResponse);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment