Skip to content

Instantly share code, notes, and snippets.

@adampiotrowski
Created January 8, 2018 20:41
Show Gist options
  • Select an option

  • Save adampiotrowski/a5fd7105f614a27b9298bdb093ff8edb to your computer and use it in GitHub Desktop.

Select an option

Save adampiotrowski/a5fd7105f614a27b9298bdb093ff8edb to your computer and use it in GitHub Desktop.
<?php
class WellCommerceClient
{
public function __construct($url, $apikey)
{
$this->url = $url;
$this->key = $apikey;
$this->id = 1;
}
public function __call($method, $params)
{
if (is_array($params)) {
$params = array_values($params);
} else {
throw new \Exception('Params must be given as array');
}
$request = [
'method' => $method,
'params' => $params,
'id' => $this->id,
'key' => $this->key,
];
$request = json_encode($request);
$curl = curl_init($this->url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-type: application/json',
]);
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response, true);
if ($response['id'] != $this->id) {
throw new \Exception($response);
}
if (!is_null($response['error'])) {
throw new \Exception('Request error: ' . $response['error']);
}
return $response['result'];
}
public function debug($Data)
{
echo "<pre>";
print_r($Data);
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment