Skip to content

Instantly share code, notes, and snippets.

@yusufusta
Last active September 9, 2021 14:28
Show Gist options
  • Select an option

  • Save yusufusta/025f38acbe9233adc35dd1dce625595c to your computer and use it in GitHub Desktop.

Select an option

Save yusufusta/025f38acbe9233adc35dd1dce625595c to your computer and use it in GitHub Desktop.
kargola.com Better API class
<?php
class Kargola
{
/**
* @var string
*/
public $Kargola = "http://185.241.100.221:9999/restapi/client"; # Default Kargola
/**
* @var string
*/
private $ApiKey = "";
/**
* @var string
*/
private $From = "";
/**
* @var string
*/
public $Url = "";
/**
* @param $apiKey
* @param $from
*/
public function __construct($apiKey, $from)
{
$this->ApiKey = $apiKey;
$this->From = $from;
$this->Curl = curl_init();
$this->Url = $this->Kargola . "/";
curl_setopt($this->Curl, CURLOPT_HTTPHEADER, ['Authorization: ' . $this->ApiKey, 'From: ' . $this->From]);
curl_setopt($this->Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYHOST, false);
}
/**
* @param $method
* @param $args
*/
public function __call($method, $args)
{
$method = strtoupper($method);
$this->Url .= $args[0];
if ($method == "GET") {
curl_setopt($this->Curl, CURLOPT_CUSTOMREQUEST, "GET");
if (!empty($args[1])) {
$this->Url = sprintf("%s?%s", $this->Url, http_build_query($args[1]));
}
} else {
curl_setopt($this->Curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($args[1])) {
curl_setopt($this->Curl, CURLOPT_POSTFIELDS, http_build_query($args[1]));
}
}
curl_setopt($this->Curl, CURLOPT_URL, $this->Url);
$response = curl_exec($this->Curl);
$err = curl_error($this->Curl);
if ($err) {
throw new \Exception($err);
}
curl_close($this->Curl);
return json_decode($response, true);
}
/**
* @return mixed
*/
public function listShipment(): array
{
return $this->GET("consignments");
}
/**
* @param array $data
* @return mixed
*/
public function addShipment(array $data): array
{
return $this->POST("consignment/add", $data);
}
/**
* @param string $shipmentBarcode
* @param array $data
* @return mixed
*/
public function editShipment(string $shipmentBarcode, array $data): array
{
return $this->PUT("consignment/edit/$shipmentBarcode", $data);
}
/**
* @param string $shipmentBarcode
* @return mixed
*/
public function deleteShipment(string $shipmentBarcode)
{
return $this->DELETE("consignment/delete/$shipmentBarcode");
}
}
<?php
# örnek kullanım:
require __DIR__ . "/KargolaAPI.php" # Bir önceki dosya
$Kargola = new Kargola("API KEY", "[email protected]"); # API KEY, FROM
# Örnek Data
$data = [];
$data["customer"] = 'Adı Soyadı';
$data["province_name"] = 'İstanbul';
$data["county_name"] = 'Kadıköy';
$data["address"] = 'Adres';
$data["telephone"] = '02161234567';
$data["branch_code"] = 100;
$data["barcode"] = 123456;
$data["amount"] = 100;
$data["summary"] = 'Açıklama';
$data["quantity"] = 1;
$data["amount_type_id"] = 6;
$data["add_service_type_id"] = 2;
$data["order_number"] = 1;
$data["output_number"] = 1;
$data["seller"] = "Satıcı";
var_dump($Kargola->listShipment()); # Kargoları Getirir
var_dump($Kargola->addShipment($data)); # Kargo Ekler
var_dump($Kargola->editShipment("123", $data)); # Kargo Düzenler
var_dump($Kargola->deleteShipment("123")); # Kargo Siler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment