在自定义模块中扩展 Magento REST API [英] Extend Magento REST API in custom module
问题描述
我想使用 Magento Rest-Api 将自定义数据添加到 Magento 表中.我在 Magento db 中添加了一张表,并使用以下链接创建了带有 Rest API 的模块
I want to use Magento Rest-Api to add custom data into Magento table. I have added one table into Magento db and created module with Rest API for that using following link
现在我想使用 Rest API 将数据添加到 Magento 表中...
Now I want to add data into Magento table using Rest API...
我需要在 api.xml
/api2.xml
或 V1.php
文件中更改的内容.
what I need to changed in api.xml
/api2.xml
or in V1.php
file.
请帮助我我已经使用产品 api2.xml 文件的参考尝试了许多代码.但没有运气.
Kindly help me I have tried many codes using reference of product api2.xml file. but no luck.
当我运行以下网址时
http://magento-host/api/rest/magepim/products/count
它将执行 V1.php
文件的 _retrieve()
函数,但是如何使用 PHP RestApi oauth 调用
_create()
函数
it will executed V1.php
file's _retrieve()
function but how to call _create()
function using PHP RestApi oauth
推荐答案
magento\app\code\core\Mage\Api2\Model\Resource.php仅允许创建方法的集合操作类型..因此在 api2.xml 文件中更改并在属性标记中设置必填字段
magento\app\code\core\Mage\Api2\Model\Resource.php is only allowed collection action type for create method.. so changed in api2.xml file and setup required fields in attribute tag
magento\app\code\community\MagePim\Extapi\etc\api2.xml
magento\app\code\community\MagePim\Extapi\etc\api2.xml
<?xml version="1.0"?>
<config>
<api2>
<resource_groups>
<extapi translate="title" module="api2">
<title>Custom API calls</title>
<children>
<extapi translate="title" module="api2">
<title>My Api</title>
</extapi>
</children>
</extapi>
</resource_groups>
<resources>
<extapi translate="title" module="api2">
<group>extapi</group>
<model>extapi/api2</model>
<working_model>extapi/api2</working_model>
<title>Custom Api</title>
<privileges>
<admin>
<create>1</create>
<retrieve>1</retrieve>
<update>1</update>
<delete>1</delete>
</admin>
</privileges>
<attributes>
<owner_id>Owner ID</owner_id>
<identityid>Identity ID</identityid>
<social_id>Social ID</social_id>
<status>Status</status>
<text>Text</text>
<request_timestamp>Request Time</request_timestamp>
<status_timestamp>Status Time</status_timestamp>
</attributes>
<routes>
<!-- Call For V1.php _retrieve() -->
<route_entity>
<route>/scheduler</route>
<action_type>entity</action_type>
</route_entity>
<!-- Call For V1.php _create() -->
<route_collection>
<route>/scheduler/create</route>
<action_type>collection</action_type>
</route_collection>
</routes>
<versions>1</versions>
</extapi>
</resources>
</api2>
</config>
magento\app\code\community\MagePim\Extapi\Model\Api2\Rest\Admin\V1.php
magento\app\code\community\MagePim\Extapi\Model\Api2\Rest\Admin\V1.php
/**
* Override for Magento's REST API
*/
class Magepim_Extapi_Model_Api2_Rest_Admin_V1 extends Mage_Api2_Model_Resource {
protected function _retrieve(){
return json_encode($shedulerData);
}
protected function _create($shedulerData){
return json_encode($shedulerData);
}
protected function _retrieveCollection(){
return json_encode(array('method'=>'_retrieveCollection'));
}
....................
}
这篇关于在自定义模块中扩展 Magento REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!