magnento2 扩展授权
获取magento2的接口授权
官方文档
https://devdocs.magento.com/guides/v2.3/get-started/authentication/gs-authentication-token.html
登录magento后台
点击 system
点击 Integrations
点击 Add New Integration
填写 扩展信息 并给予扩展权限
点击 save
新的扩展建立完成
但是现在状态是 Inactive
所以我们需要点击 对应扩展的 Activate 栏下的 Activate 以开启扩展
开启后弹出窗口
我们可以把这些数据都记录下
Access Token 这个很重要
写了调用API 可参考
class Magento2
{
/*************************************** Magento 2 API ***************************************************/
/**
授权获取
https://blog.csdn.net/huangkang1995/article/details/111715480
*/
public $consumer_key = "";
public $consumer_secret = "";
public $access_token = "";
public $access_token_key = "";
public $storeViewCode = "default"; //商店视图code 需要修改 通过store/storeViews 获取商店全部视图 未对应会报 Specified request cannot be processed.
public $base_url = "网址/rest/";
public $isShowUrl = false;
// 回调方法
function __call($method,$args)
{
//$method = strtolower($method);
$pathUrl = $method;
$where = $args[0];
$equest = $args[1];
!$equest && $equest = "GET";
$data = $this->superMethod($pathUrl,$where,$equest);
return $data;
}
/**
* 公共方法 规整curl标准
* 黄康 2020-11-13
*/
function superMethod($pathUrl,$where=[],$method="POST")
{
$pathUrl = $this->base_url.$this->storeViewCode."/V1/".$pathUrl;
if($method == "GET")
{
$where = http_build_query($where);
$where && $pathUrl = $pathUrl."?".$where;
$where = [];
}
$data = $this->jsonCurl($pathUrl,$where,$method);
$data = json_decode($data,true);
return $data;
}
/*
function __get($name)
{
}
function __set($name,$val)
{
}
*/
function jsonCurl($url,$info=array(),$method="POST",$headers= [])
{
// 显示运行链接
if($this->isShowUrl)
echo $url."<br>";
//初始化
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $url);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
$headers[] = "Authorization: Bearer ".$this->access_token;
$headers[] = "Content-Type:application/json;";
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers );
if($method != "GET")
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($info));
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
$response = curl_exec($connection);
curl_close($connection);
return $response;
}
}
使用调用接口示例
class Sample extends Common
{
/**
* storeList
*/
public function storeViews()
{
$magento2 = new Magento2();
$magento2->isShowUrl = true;
$data = $magento2->superMethod("store/storeViews",[],"GET");
dump($data);
}
/**
* 订单
*/
public function orders()
{
$magento2 = new Magento2();
$where = [];
$where["searchCriteria"]["currentPage"] = 1;
$where["searchCriteria"]["pageSize"] = 2;
$magento2->isShowUrl = true;
$data = $magento2->orders($where);
dump($data);
}
/**
* 产品
*/
public function products()
{
$magento2 = new Magento2();
$where = [];
$where["searchCriteria"]["currentPage"] = 1;
$where["searchCriteria"]["pageSize"] = 2;
$magento2->isShowUrl = true;
$data = $magento2->products($where);
dump($data);
}
}
/rest/default/V1/ API默认已经填写
magento2 接口开发文档
https://magento.redoc.ly/2.3.6-admin/tag/storewebsites
magento2 接口格式为 products
可直接使用
m
a
g
e
n
t
o
2
−
>
p
r
o
d
u
c
t
s
(
magento2->products(
magento2−>products(where);
如上面示例 搜索 products() 方法
magento2 接口格式为 store/websites
调用方式为 $magento2->superMethod(“store/storeViews”,[],“GET”);
如上面示例 搜索 storeViews() 方法