Extend Magento REST API in custom module

2019-03-21 14:37发布

问题:

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

http://web.archive.org/web/20130512072025/http://magepim.com/news/Extending-the-Magento-REST-API-part-1_13

Now I want to add data into Magento table using Rest API...

what I need to changed in api.xml/api2.xml or in V1.php file.

Kindly help me I have tried many codes using reference of product api2.xml file. but no luck.

when I run following URL

http://magento-host/api/rest/magepim/products/count

it will executed V1.php file's _retrieve() function but how to call _create() function using PHP RestApi oauth

回答1:

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

<?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

/**
 * 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'));
    }
....................
}