php – Magento中的Custom Rest Api

前端之家收集整理的这篇文章主要介绍了php – Magento中的Custom Rest Api前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要休息api来创建磁铁客户,因为我遵循了本教程 http://www.authenticdesign.co.uk/extending-magento-rest-api-v2/

我一步一步地跟着它,但是当我在休息客户端测试api时,它给了我:{“messages”:{“error”:[{“code”:404,“message”:“请求与任何路由都不匹配. “}]}}

我不知道我在哪里弄错了.帮帮我,因为我对magento和PHP都很新.

步骤是:

1.在(app / etc / module / Custom_Restapi.xml)启用扩展

  1. <config>
  2. <modules>
  3. <Custom_Restapi>
  4. <active>true</active>
  5. <codePool>local</codePool>
  6. </Custom_Restapi_Groups>
  7. </modules>
  8. </config>

2. config.xml at(app / code / local / Custom / Restapi / etc / config.xml)

  1. <?xml version="1.0"?>
  2. <config>
  3. <modules>
  4. <Custom_Restapi>
  5. <version>0.1.0.0</version>
  6. </Custom_Restapi>
  7. </modules>
  8. <global>
  9. <models>
  10. <restapi>
  11. <class>Custom_Restapi_Model</class>
  12. </restapi>
  13. </models>
  14. </global>
  15. </config>

3. api2.xml at(app / code / local / Custom / Restapi / etc / api2.xml)

  1. <?xml version="1.0"?>
  2. <config>
  3. <api2>
  4. <resource_groups>
  5. <restapi translate="title" module="Custom_Restapi">
  6. <title>Custom Rest API</title>
  7. <sort_order>10</sort_order>
  8. </restapi>
  9. </resource_groups>
  10. <resources>
  11. <restapi translate="title" module="Custom_Restapi">
  12. <group>restapi</group>
  13. <model>restapi/api2_restapi</model>
  14. <title>Testing My Rest API</title>
  15. <sort_order>10</sort_order>
  16. <privileges>
  17. <admin>
  18. <create>1</create>
  19. </admin>
  20. </privileges>
  21. <attributes translate="" module="Custom_Restapi">
  22. <firstname>First Name</firstname>
  23. <lastname>Last Name</lastname>
  24. <email>Email</email>
  25. <password>Password</password>
  26. </attributes>
  27. <routes>
  28. <route>
  29. <route>/customer</route>
  30. <action_type>collection</action_type>
  31. </route>
  32. </routes>
  33. <versions>1</versions>
  34. </restapi>
  35. </resources>
  36. </api2>
  37. </config>

4.模型类Restapi.PHP at(app / code / local / Custom / Restapi / Model / Api2 / Restapi.PHP)

  1. <?PHP
  2.  
  3. class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
  4. {
  5.  
  6. }
  7.  
  8. ?>

5. V1.PHP at(app / code / local / Custom / Restapi / Model / Api2 / Restapi / Rest / Admin / V1.PHP)

  1. <?PHP
  2. class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi
  3. {
  4.  
  5. /**
  6. * Create a customer
  7. * @return array
  8. */
  9.  
  10. public function _create() {
  11.  
  12. $requestData = $this->getRequest()->getBodyParams();
  13. $firstName = $requestData['firstname'];
  14. $lastName = $requestData['lastname'];
  15. $email = $requestData['email'];
  16. $password = $requestData['password'];
  17.  
  18. $customer = Mage::getModel("customer/customer");
  19.  
  20. $customer->setFirstname($firstName);
  21. $customer->setLastname($lastName);
  22. $customer->setEmail($email);
  23. $customer->setPasswordHash(md5($password));
  24. $customer->save();
  25.  
  26. return json_encode(array("testing","Success"));
  27. }
  28.  
  29. }
  30. ?>

我的网址就像:baseurl / api / rest / customer

我会把它放在评论中,因为我觉得这不是一个完全完整的答案,但我还没有被允许.一些东西:

> config.xml中的全局标记关闭.
>您无法使用引用实体的网址创建记录
必须使用route_collection中定义的收集路由
api2.xml中的节点.所以你应该打电话给/ api / rest / customer.
>自该方法以来,无需单独的“创建”路径
由http方法(post / get / delete / etc)和正文选择
内容.我会推荐一条“customer /:id”的路线
route_entity元素.所以也要确保你提交的是HTTP POST.

我无法重现您发布的确切错误,但在纠正上述项目后我能够正常工作.

此外,请确保在管理区域中授予此资源的权限,并清除Web服务配置缓存.

您列出的特定异常将在路由方法的Mage_Api2_Model_Router中引发.

我重写了这个并在github上创建了一个带有工作模块的repo:https://github.com/themizzi/Custom-Magento-Rest-Api2.该模块使用Guest访问权限,因为我没有时间完成整个oAuth交易,但是如果您只是将api2.xml中的guest虚拟机节点更新为admin并在管理区域更新您的访问权限,它将工作.

猜你在找的PHP相关文章