我想创建我的自定义REST api。 我已经创建了自己的模块Custom/Restapi.Custom [Namespace],Restapi [Module name]。如何在magento中实现自定义REST API?
在etc文件夹中,我创建了config.xml和api2.xml。下面是代码 - :
的Config.xml
<?xml version="1.0"?>
<config>
<modules>
<Custom_Restapi>
<version>0.1.0.0</version>
</Custom_Restapi>
</modules>
<global>
<models>
<restapi>
<class>Custom_Restapi_Model</class>
</restapi>
</models>
</config>
api2.xml
<config>
<api2>
<resource_groups>
<restapi translate="title" module="Custom_Restapi">
<title>Custom Rest API</title>
<sort_order>10</sort_order>
</restapi>
</resource_groups>
<resources>
<restapi translate="title" module="Custom_Restapi">
<group>restapi</group>
<model>restapi/api2_restapi</model>
<title>Testing My Rest API</title>
<sort_order>10</sort_order>
<privileges>
<admin>
<create>1</create>
<!-- <retrieve>1</retrieve>
<delete>1</delete>-->
</admin>
<!-- <customer>
<create>1</create>
<retrieve>1</retrieve>
<delete>1</delete>
</customer>
<guest>
<create>1</create>
<retrieve>1</retrieve>
<delete>1</delete>
</guest>-->
</privileges>
<routes>
<route_entity>
<route>/custom/createwebsite/:s</route>
<action_type>entity</action_type>
</route_entity>
</routes>
<versions>1</versions>
</restapi>
</resources>
</api2>
型号目录结构
应用程序\代码\本地\C ustom \ RESTAPI \型号\ API2 \ Restapi.php。以下是文件级代码:
Restapi.php
class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{
}
应用程序\代码\本地\自定义\ RESTAPI \型号\ API2 \ RESTAPI \休息\管理\ V1.php下面的代码文件级:
V1.php
class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
{
protected function _create(){
return json_encode(array("testing","hello"));
}
protected function _retrieveCollection()
{
return json_encode(array("testing","hello"));
}
}
ETC /模块配置文件也设置。
管理员设置
我已创建的OAuth角色admin。在左侧标签下,“角色Api资源”模块设置可见并被选中。
其他消费者设置也被配置。
下面是REST API调用脚本代码 - :
阿比调用脚本代码
$consumerKey = 'ozr74egldg07dpxtkk9uq1o8bj6wwd65'; // from Admin Panel's "REST - OAuth Consumers page"
$consumerSecret = 'ozr74egldg07dpxtkk9uq1o8bj6wwd65'; // from Admin Panel's "REST - OAuth Consumers page"
// Set the OAuth callback URL to this script since it contains the logic
// to execute *after* the user authorizes this script to use the Coupon AutoGen API
$callbackUrl = "http://127.0.0.1/magento/testscript.php";
// Set the URLs below to match your Magento installation
$temporaryCredentialsRequestUrl = "http://127.0.0.1/mage_restapi/oauth/initiate?oauth_callback=" . urlencode($callbackUrl);
$adminAuthorizationUrl = 'http://127.0.0.1/mage_restapi/admin/oauth_authorize';
$accessTokenRequestUrl = 'http://127.0.0.1/mage_restapi/oauth/token';
$apiUrl = 'http://127.0.0.1/mage_restapi/api/rest';
session_start();
if (!isset($_GET['oauth_token']) && isset($_SESSION['state']) && $_SESSION['state'] == 1) {
$_SESSION['state'] = 0;
echo "try";
}
try {
$authType = ($_SESSION['state'] == 2) ? OAUTH_AUTH_TYPE_AUTHORIZATION : OAUTH_AUTH_TYPE_URI;
$oauthClient = new OAuth($consumerKey, $consumerSecret, OAUTH_SIG_METHOD_HMACSHA1, $authType);
$oauthClient->enableDebug();
if (!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$requestToken = $oauthClient->getRequestToken($temporaryCredentialsRequestUrl);
$_SESSION['secret'] = $requestToken['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: ' . $adminAuthorizationUrl . '?oauth_token=' . $requestToken['oauth_token']);
exit;
} else if ($_SESSION['state'] == 1) {
$oauthClient->setToken($_GET['oauth_token'], $_SESSION['secret']);
$accessToken = $oauthClient->getAccessToken($accessTokenRequestUrl);
$_SESSION['state'] = 2;
$_SESSION['token'] = $accessToken['oauth_token'];
$_SESSION['secret'] = $accessToken['oauth_token_secret'];
header('Location: ' . $callbackUrl);
exit;
} else {
// We have the OAuth client and token. Now, let's make the API call.
$oauthClient->setToken($_SESSION['token'], $_SESSION['secret']);
// Generate coupon codes via POST
$resourceUrl = "$apiUrl/custom";
$oauthClient->fetch($resourceUrl, OAUTH_HTTP_METHOD_POST, array(
'Accept' => 'application/json',
'Content-Type' => 'application/json',
));
$data= json_decode($oauthClient->getLastResponse(), true);
echo "Data is:<br/>".$data;
}
} catch (OAuthException $e) {
print_r($e->getMessage());
//echo "<br/>";
//print_r($e->lastResponse);
}
当我试图访问API则要求授权
授权应用程序管理员请求访问您的帐户
授权后应用程序将有权访问您的帐户。
授权按钮和拒绝按钮
点击授权按钮错误 - 后:
无效的授权/坏请求(有一个404,预期HTTP/1。1 20X或重定向) { “消息”:{ “错误”:[{ “代码”:404, “消息”: “请求不匹配任何路线”}]}}
PHP OAuth的扩展不支持RSA-SHA1支持我的OAuth设置
http://ctodilemma.com/2013/04/customising-and-extending-the-magento-rest-api/
我们只用通过的Magento提供的功能来访问在休息的代码,下面是功能列表 - :
- _create()
- _retrieve()
- _delete()
- _retrieveCollection()
- _Update()
- _multiUpdate()
- _multiDelete
错误 验证/错误请求无效(得到404 ,预期HTTP/1.1 20X或重定向) { “消息”:{ “错误”:[{ “代码”:404, “消息”: “请求不匹配任何路线”}]}}
我上面分享了我的所有分析,但我无法访问这些数据。请分享您的反馈。