我正在尝试将 DHL 电子商务 API 与我的 Laravel 集成。我曾尝试破解 WordPress WooCommerce 代码,但觉得这是浪费时间。经过我的不断努力,我破解了OpenCart的代码,得到了创建发货草稿的代码。但实际上,我想通过API创建发货标签,但OpenCart不提供此功能,而WooCommerce有。有人可以帮助我吗?
DHL API 集成的可用文档 Manual_WooCommerce_plug-in_EN.pdf
澄清一下,
下面是opencart的破解示例代码,请帮我创建
CreateShipmentLabel
函数
<?php
class ControllerExtensionShippingDhlparcel
{
private $db;
private $url = "https://connector.dhlparcel.nl/api/V2/webhook/.............";
public function __construct()
{
$this->db = new mysqli('localhost', 'root', '', 'opencart');
}
public function webhook($orderStatusId = 2)
{
$order = $this->getOrder(1);
$data_string = json_encode($order);
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch,CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string),
'dhlparcel-connector: opencart',
]
);
curl_exec($ch);
}
public function getOrder(int $order_id): array
{
return $shipment_data;
}
public function CreateShipmentLabel(){
// please help me create this function
}
}
$model = new ControllerExtensionShippingDhlparcel();
$model->webhook();
您可以在这里浏览他们的文档:https://developer.dhl.com/api-catalog
话虽如此,我认为你应该更多地重构你的代码。 Laravel 已经提供了一个内置的 HTTP 客户端,您可以使用它来代替普通的旧式 curl 函数,而且您最好使用 Eloquent ORM 而不是
mysqli
我个人的做法是在服务容器中创建一个 HTTP 宏,在仅与 DHL API 交互的服务中使用该 HTTP 宏,然后在您的应用程序中使用该服务。这样您就可以非常轻松地进行调整和维护。
同样适用于
getOrder
。你正在声明你不需要的论据。