当前位置: 技术文章>> Magento 2:如何使用REST API获取订单详细信息

文章标题:Magento 2:如何使用REST API获取订单详细信息
  • 文章分类: Magento
  • 17915 阅读
系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》

本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。


在Magento 2中使用REST API获取订单详细信息的步骤:

步骤1:在Magento根文件夹中创建getOrderDetails.php脚本文件并添加以下代码

<?php
$userData = array("username" => "admin", "password" => "adminPassword");
$baseUrl = "http://127.0.0.1/magento24/index.php"; // your magento base url
$ch = curl_init($baseUrl."/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
 
$token = curl_exec($ch);
 
$ch = curl_init($baseUrl."/rest/V1/orders/1");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
 
$result = curl_exec($ch);
 
$result = json_decode($result, 1);
echo '<pre>';print_r($result);?>

注意 - 您可以在Magento文件系统中的任何位置使用此代码。例如:您也可以在自定义模块中使用它。

结论:

通过这种方式,您可以使用Magento 2中的REST API获取订单详细信息。除此之外,您还可以使用Magento 2中的REST API获取订单状态。


推荐文章