Skip to content

Instantly share code, notes, and snippets.

@mrkhoa99
Created January 16, 2017 13:09
Show Gist options
  • Save mrkhoa99/4262c1a28ba9244283494cbb18f45f47 to your computer and use it in GitHub Desktop.
Save mrkhoa99/4262c1a28ba9244283494cbb18f45f47 to your computer and use it in GitHub Desktop.
<?php
class ApiClient
{
const METHOD_GET = 'GET';
const METHOD_PUT = 'PUT';
const METHOD_POST = 'POST';
const METHOD_DELETE = 'DELETE';
protected $validMethods = [
self::METHOD_GET,
self::METHOD_PUT,
self::METHOD_POST,
self::METHOD_DELETE,
];
protected $apiUrl;
protected $cURL;
protected $token;
public function __construct($apiUrl, $username, $apiKey)
{
$this->apiUrl = rtrim($apiUrl, '/') . '/';
$this->cURL = curl_init($this->apiUrl."rest/V1/integration/admin/token");
$userData = array(
'username' => $username,
'password' => $apiKey
);
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($this->cURL, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$this->token = curl_exec($this->cURL);
}
public function call($url, $method = self::METHOD_GET, $data = [], $params = [])
{
if (!in_array($method, $this->validMethods)) {
throw new Exception('Invalid HTTP-Methode: ' . $method);
}
$queryString = '';
if (!empty($params)) {
$queryString = http_build_query($params);
}
//$url = rtrim($url, '?') . '?';
$url = $this->apiUrl . $url;// . $queryString;
//$dataString = json_encode($data);
$this->cURL = curl_init($url);
curl_setopt($this->cURL, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($this->cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->cURL, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($this->cURL, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($this->token)));
$result = curl_exec($this->cURL);
$httpCode = curl_getinfo($this->cURL, CURLINFO_HTTP_CODE);
return $this->prepareResponse($result, $httpCode);
}
public function get($url, $params = [])
{
return $this->call($url, self::METHOD_GET, [], $params);
}
public function post($url, $data = [], $params = [])
{
return $this->call($url, self::METHOD_POST, $data, $params);
}
public function put($url, $data = [], $params = [])
{
return $this->call($url, self::METHOD_PUT, $data, $params);
}
public function delete($url, $params = [])
{
return $this->call($url, self::METHOD_DELETE, [], $params);
}
protected function prepareResponse($result, $httpCode)
{
echo "<h2>HTTP: $httpCode</h2>";
if (null === $decodedResult = json_decode($result, true)) {
$jsonErrors = [
JSON_ERROR_NONE => 'No error occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been reached',
JSON_ERROR_CTRL_CHAR => 'Control character issue, maybe wrong encoded',
JSON_ERROR_SYNTAX => 'Syntaxerror',
];
echo '<h2>Could not decode json</h2>';
echo 'json_last_error: ' . $jsonErrors[json_last_error()];
echo '<br>Raw:<br>';
echo '<pre>' . print_r($result, true) . '</pre>';
return;
}
if (isset($decodedResult)) {
echo '<pre>' . print_r($decodedResult, true) . '</pre>';
}
return $decodedResult;
}
}
$client = new ApiClient('http://127.0.0.1/m2/','web','admin123');
//$client->get('rest/V1/orders/47'); //get Order
$userData = [
"entity"=> [
"order_id"=> 47,
"parent_id"=> 47,
//"entity_id"=> 47,
"weight"=> 11,
"qty"=> 1,
"description"=> "Tracking code Descripton news test",
"track_number"=> "1234567324654",
"title"=> "Custom Tracking Code",
"carrier_code"=> "fedex"
]
];
$entity = $userData;
$client->post('rest/V1/shipment/track',$entity);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment