需要在代码级调用Magento2 rest api如何调用默认的magento REST APIS
需要在代码级调用Magento2 rest api如何调用默认的magento REST APIS
Answers:
Magento 2支持REST(表示状态传输)和SOAP(简单对象访问协议),就像我们以前使用的Magento的旧版本一样。官方文档主要基于原始curl请求,而没有使用某些特定语言的示例。PHP是我们的工作,并且也会有很多人在使用它,因此我们尝试为您提供有关如何连接和使用Magento 2 API的真实PHP示例。
Magento中有三种可以访问API的用户类型,分别是:
1)来宾用户他们可以访问具有匿名权限的资源。
2)管理员/集成他们可以访问配置授权的资源。
3)客户他们可以自行或匿名访问资源。
我们可以使用三种类型的身份验证:1)基于令牌的身份验证
这里的想法是在初始连接期间提供用户名和密码,并接收用于后续请求的令牌,直到令牌过期。
这是通过PHP使用rest API的示例
<?php
$userData = array("username" => "USERNAMe", "password" => "PASSWORD");
$ch = curl_init("http://www.YOUR_DOMIAN.com/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("http://www.YOUR_DOMIAN.com/rest/V1/products/LC515");
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);
var_dump($result);
注意:-确保用户名是具有适当资源的管理员用户
基于OAuth的身份验证
通过OAuth 1.0a(https://en.wikipedia.org/wiki/OAuth)允许访问API 。在这种情况下,请将Magento API视为一项服务,该服务允许通过获得资源所有者的批准来访问第三方的资源。例如,从第三方应用程序(客户端)从Magento API(服务)获取客户(资源所有者)信息。这有点超出本文的范围,并且正在准备单独的文章,但是有一个使用集成的简单示例,其中没有“ Identity link URL”和“ Callback URL”。
您需要做的是进入“系统”>“集成”,然后添加没有“身份链接URL”和“回调URL”的新集成。切记在“ API”选项卡上编辑资源访问。
然后运行以下脚本:
<?php
function sign($method, $url, $data, $consumerSecret, $tokenSecret)
{
$url = urlEncodeAsZend($url);
$data = urlEncodeAsZend(http_build_query($data, '', '&'));
$data = implode('&', [$method, $url, $data]);
$secret = implode('&', [$consumerSecret, $tokenSecret]);
return base64_encode(hash_hmac('sha1', $data, $secret, true));
}
function urlEncodeAsZend($value)
{
$encoded = rawurlencode($value);
$encoded = str_replace('%7E', '~', $encoded);
return $encoded;
}
// REPLACE WITH YOUR ACTUAL DATA OBTAINED WHILE CREATING NEW INTEGRATION
$consumerKey = '0b3d76jra3log3i2dthslvf0fg54avjv';
$consumerSecret = 'yk8ypwrqrjepa01g3bt0f57ii6kgkqb4';
$accessToken = 'pjhpgtv3un78h83wmhcbk0s8nuxbfhx0';
$accessTokenSecret = 'ryf7sqesnrsn23aifragqywttbm3gkch';
$method = 'GET';
$url = 'http://www.YOUR_DOMIAN..com/rest/V1/products/LC515';
//
$data = [
'oauth_consumer_key' => $consumerKey,
'oauth_nonce' => md5(uniqid(rand(), true)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $accessToken,
'oauth_version' => '1.0',
];
$data['oauth_signature'] = sign($method, $url, $data, $consumerSecret, $accessTokenSecret);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => [
'Authorization: OAuth ' . http_build_query($data, '', ',')
]
]);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
我们需要使用searchCriteria ex的少量API:
GET http://<magento_host>/index.php/rest/V1/products?
searchCriteria[filter_groups][0][filters][0][field]=name&
searchCriteria[filter_groups][0][filters][0][value]=%25Leggings%25&
searchCriteria[filter_groups][0][filters][0][condition_type]=like&
searchCriteria[filter_groups][0][filters][1][field]=name&
searchCriteria[filter_groups][0][filters][1][value]=%25Parachute%25&
searchCriteria[filter_groups][0][filters][1][condition_type]=like
请参阅 http://devdocs.magento.com/guides/v2.1/howdoi/webapi/search-criteria.html
要完成已经给出的示例,但使用url中的get参数,请使用:
$consumerKey = '2f5jp18p92au5bdfjwp686_somemore';
$consumerSecret = 'x1b48mz2pg1do7gi42aqeb1_somemore';
$accessToken = 'o5msvni7j79t79tl09u18x6_somemore';
$accessTokenSecret = 'f9uhqbf4ym6rv4jdxyymt0u_somemore';
function sign($method, $url, $data, $get, $consumerSecret, $tokenSecret){
$data = array_merge($data,$get);
ksort($data);
$url = str_replace('%7E', '~', rawurlencode($url));
$data = str_replace('%7E', '~', rawurlencode(http_build_query($data, '', '&')));
$data = implode('&', [$method, $url, $data]);
$secret = implode('&', [$consumerSecret, $tokenSecret]);
return base64_encode(hash_hmac('sha1', $data, $secret, true));
}
$method = 'GET';
$url = 'https://your.host/rest/V1/customers/search';
$params =[
'search_criteria[filter_groups][0][filters][0][field]' => 'firstname',
'search_criteria[filter_groups][0][filters][0][value]' => '%dallas%',
'search_criteria[filter_groups][0][filters][0][condition_type]' => 'like',
'search_criteria[filter_groups][1][filters][0][field]' => 'lastname',
'search_criteria[filter_groups][1][filters][0][value]' => '%clarke%',
'search_criteria[filter_groups][1][filters][0][condition_type]' => 'like',
];
//$url = 'https://your.host/rest/V1/products/sku';
//$params = [];
$data = [
'oauth_consumer_key' => $consumerKey,
'oauth_nonce' => md5(uniqid(rand(), true)),
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_token' => $accessToken,
'oauth_version' => '1.0',
];
$data['oauth_signature'] = sign($method, $url, $data, $params, $consumerSecret, $accessTokenSecret);
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => $url.'?'.http_build_query($params),
CURLOPT_HTTPHEADER => [
'Authorization: Basic '. base64_encode('not:telling'),
'Authorization: OAuth ' . http_build_query($data, '', ',')
]
]);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);