Magento2 Soap 服务不工作

Magento2 Soap service not working

我试图创建一个 soap 服务,但不知何故它的 wsdl url 不工作。以下是代码:- Api/CustomapitInterface.php

namespace W3solver\Customapi\Api;

interface CustomapiInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name Users name.
     * @return string Greeting message with users name.
     */
    public function name($name);
}

model/Customapi.php

    <?php
namespace W3solver\Customapi\Model;
use W3solver\Customapi\Api\CustomapiInterface;

class Customapi implements CustomapiInterface
{
    /**
     * Returns greeting message to user
     *
     * @api
     * @param string $name Users name.
     * @return string Greeting message with users name.
     */
    public function name($name) {
        return "Hello, " . $name;
    }
}

etc/di.xml

 <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="W3solver\Customapi\Api\CustomapiInterface" type="W3solver\Customapi\Model\Customapi" />
</config>

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="W3solver_Customapi" setup_version="1.0.0" />
</config>

etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/customapi/name/:name" method="GET">
        <service class="W3solver\Customapi\Api\CustomapiInterface" method="name"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

我使用来自 http://inchoo.net/magento/api-magento/magento-2-custom-api/ 的引用创建了这个。这个我没看错。

下面是我正在尝试使用的url:- http://magento2.local/index.php/soap/default?wsdl&services=w3solverCustomapiV1

Magento 2 默认禁用 API 匿名访问 API,您需要从后端管理面板启用它。

要禁用此功能,请登录管理面板并select

Stores > Configuration > Services > Magento Web API > Web API Security.

然后 select 从“允许匿名访客访问”菜单选择是。

您可以在开发指南中找到更多信息 here