自REST API在Magento [英] Custom Rest Api in Magento

查看:193
本文介绍了自REST API在Magento的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要REST API在磁创造客户为我跟着这个教程的 http://www.authenticdesign.co.uk/extending-magento-rest-api-v2/

I need rest api to create customer in magneto for that I followed this tutorial http://www.authenticdesign.co.uk/extending-magento-rest-api-v2/

我跟着它一步一步,但是当我测试其他客户端的API,它给我: {消息:{错误:[{code:404, 消息:请求不符合任何路线}]}}

I followed it step by step, But When I test the api on rest client it give me: {"messages":{"error":[{"code":404,"message":"Request does not match any route."}]}}

我没有在那里我犯错误的想法。帮我在这里,因为我很新的Magento以及为PHP。

I do not have any idea where I am making mistake. Help me out here as I am very new to magento as well as for php.

的步骤是:

1。在启用扩展(应用程序的/ etc /模块/ Custom_Restapi.xml)

<config>
    <modules>
        <Custom_Restapi>
            <active>true</active>
            <codePool>local</codePool>
        </Custom_Restapi_Groups>
    </modules>
</config>

2。在config.xml文件(应用程序/ code /本地/自定义/ RESTAPI的/ etc / 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>
    </global>
</config>

3。 api2.xml在(APP / code /本地/自定义/ RESTAPI的/ etc / api2.xml)

<?xml version="1.0"?>
<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>
                    </admin>
                </privileges>
                <attributes  translate="" module="Custom_Restapi">
                    <firstname>First Name</firstname>
                    <lastname>Last Name</lastname>
                    <email>Email</email>
                    <password>Password</password>
                </attributes>
               <routes>
                    <route>
                        <route>/customer</route>
                        <action_type>collection</action_type>
                    </route>
                </routes>
                <versions>1</versions>
            </restapi>
        </resources>
    </api2>
</config>

4。模型类Restapi.php在(APP / code /本地/自定义/ RESTAPI /型号/ API2 / Restapi.php)

<?php

class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{

}

?>

5。 V1.php在(APP / code /本地/自定义/ RESTAPI /型号/ API2 / RESTAPI / REST /行政/ V1.php)

<?php
class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
{

    /**
     * Create a customer
     * @return array
     */

    public function _create() {

        $requestData = $this->getRequest()->getBodyParams();
        $firstName = $requestData['firstname'];
        $lastName = $requestData['lastname'];
        $email = $requestData['email'];
        $password = $requestData['password'];

        $customer = Mage::getModel("customer/customer");

        $customer->setFirstname($firstName);
        $customer->setLastname($lastName);
        $customer->setEmail($email);
        $customer->setPasswordHash(md5($password));
        $customer->save();

       return  json_encode(array("testing","Success"));
   }

}
?>

我的网址是这样的:baseURL时/ API / REST /客户

推荐答案

因为我觉得这不是一个完全完整的答案,但我还没有让我把这个注释。有几件事情:

I would put this in a comment since I feel this is not a fully complete answer, but I am not yet allowed to. A few things:


  1. 您在config.xml中全局变量没有关闭。

  1. Your global tag in config.xml is not closed.

您不能使用URL引用实体创建记录,你
必须使用在route_collection限定的收集路线
节点api2.xml。所以,你应该调用/ API / REST /客户。

You cannot create records using a url that references entities, you have to use the collection route defined in the route_collection node in api2.xml. So you should be calling /api/rest/customer.

有没有需要有一个单独的,因为该方法创造路线
通过HTTP方法(后/获取/删除/等)和身体选择
内容。我会建议的途径:为客户/ ID
route_entity元素。所以,也请确保您所提交的HTTP POST。

There is no need to have a separate "create" route since the method is chosen by the http method (post/get/delete/etc) and the body content. I would recommend a route of "customer/:id" for the route_entity element. So also be sure that you are submitting an HTTP POST.

我无法重现您发布确切的错误,但我可以纠正上述项目后,得到这个工作。

I was not able to reproduce the exact error you posted, but I was able to get this working after correcting the above items.

另外,一定要给这个资源在管理方面的权限,并清除您的Web服务配置缓存。

Also, be sure to give permission on this resource in the admin area and to clear your Web Services config caches.

您列出的特定异常被抛出在Mage_Api2_Model_Router在路由方法。

The specific exception you listed is thrown in Mage_Api2_Model_Router in the route method.

我返工这一点,并在与工作模块github上创建了一个回购:<一href=\"https://github.com/themizzi/Custom-Magento-Rest-Api2\">https://github.com/themizzi/Custom-Magento-Rest-Api2.该模块采用客户访问,因为我没有时间去通过整个OAuth的交易,但是如果你只是更新api2.xml管理员来宾节点,并更新在管理方面的访问,它会工作。

I reworked this and created a repo on github with the working module: https://github.com/themizzi/Custom-Magento-Rest-Api2. The module uses Guest access since I didn't have time to go through the whole oAuth deal, but if you simply update the guest node in api2.xml to admin and update your access in the admin area, it will work.

这篇关于自REST API在Magento的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆