简体   繁体   English

Magento 2从根文件访问Rest API接口

[英]Magento 2 access Rest API interface from root file

I have created a script file in root and I want to create a new customer from that file below is my code for that. 我已经在根目录中创建了一个脚本文件,我想从该文件中创建一个新客户,下面是我的代码。

use Magento\Framework\App\Bootstrap;
//use Magento\Customer\Api\Data\CustomerInterface;

require __DIR__ . '/../../app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();

$obj->get('Magento\Framework\App\State')->setAreaCode('frontend');

$customerData = [
        'customer' => [
            'email' => 'demo@user.com',
            'firstname' => 'John',
            'lastname' => 'Wick',
           ],
        'password' => 'John123'
    ];

$customer=$obj->get('\Magento\Customer\Api\AccountManagementInterface');
$customer->createAccount($customerData);

but when i run this code it gives me below error. 但是当我运行这段代码时,它给了我下面的错误。

Fatal error : Uncaught TypeError: Argument 1 passed to Magento\\Customer\\Model\\AccountManagement\\Interceptor::createAccount() must be an instance of Magento\\Customer\\Api\\Data\\CustomerInterface, array given, called in C:\\wamp64\\www\\mg\\m2\\rest\\v3\\Customer.php on line 82 and defined in C:\\wamp64\\www\\mg\\m2\\generated\\code\\Magento\\Customer\\Model\\AccountManagement\\Interceptor.php:124 Stack trace: 致命错误 :未捕获的TypeError:传递给Magento \\ Customer \\ Model \\ AccountManagement \\ Interceptor :: createAccount()的参数1必须是Magento \\ Customer \\ Api \\ Data \\ CustomerInterface的实例,给定数组,在C:\\ wamp64 \\ www中调用第82行上的\\ mg \\ m2 \\ rest \\ v3 \\ Customer.php,在C:\\ wamp64 \\ www \\ mg \\ m2 \\ Generated \\ code \\ Magento \\ Customer \\ Model \\ AccountManagement \\ Interceptor.php:124中定义:堆栈跟踪:

0 C:\\wamp64\\www\\mg\\m2\\rest\\v3\\Customer.php(82): Magento\\Customer\\Model\\AccountManagement\\Interceptor->createAccount(Array) 0 C:\\ wamp64 \\ www \\ mg \\ m2 \\ rest \\ v3 \\ Customer.php(82):Magento \\ Customer \\ Model \\ AccountManagement \\ Interceptor-> createAccount(Array)

1 C:\\wamp64\\www\\mg\\m2\\rest\\v3\\api.php(7): require_once('C:\\wamp64\\www\\m...') 1 C:\\ wamp64 \\ www \\ mg \\ m2 \\ rest \\ v3 \\ api.php(7):require_once('C:\\ wamp64 \\ www \\ m ...')

2 {main} 2 {main}

thrown in C:\\wamp64\\www\\mg\\m2\\generated\\code\\Magento\\Customer\\Model\\AccountManagement\\Interceptor.php on line 124 124行的C:\\ wamp64 \\ www \\ mg \\ m2 \\ Generated \\ code \\ Magento \\ Customer \\ Model \\ AccountManagement \\ Interceptor.php中抛出

Please help. 请帮忙。 actually i want to access web api method directly from code and get response so that i can modify that response accordingly. 实际上,我想直接从代码访问Web api方法并获取响应,以便我可以相应地修改该响应。 because we already have app running in magento 1.9. 因为我们已经有在magento 1.9中运行的应用程序。 so we don't want to change response 所以我们不想改变回应

It's just like the error message says. 就像错误消息说的那样。 You have to pass an implementation of Magento\\Customer\\Api\\Data\\CustomerInterface to the createAccount method. 您必须将Magento\\Customer\\Api\\Data\\CustomerInterface传递给createAccount方法。

So instead of passing a simple array like $customerData , you should create a new instance of a CustomerInterface implementation instead ... and fill it with the required data. 因此,与其传递像$customerData这样的简单数组,不如创建一个CustomerInterface实现的新实例,然后用所需的数据填充它。

Searching through their github repo I found this: Magento\\Customer\\Model\\Data\\Customer https://github.com/magento/magento2/search?utf8=%E2%9C%93&q=%22implements+Magento%5CCustomer%5CApi%5CData%5CCustomerInterface%22&type= 通过搜索他们的github存储库,我发现了这一点: Magento\\Customer\\Model\\Data\\Customer https://github.com/magento/magento2/search?utf8=%E2%9C%93&q=%22implements+Magento%5CCustomer%5CApi% 5CData%5CCustomerInterface%22&类型=

So unless you want to create your own implementation, this is what you should pass to createAccount 因此,除非您要创建自己的实现,否则应将其传递给createAccount

You should be able to create one via the factory like so: 您应该可以像这样通过工厂创建一个:

try {
    $objectManager = $bootstrap->getObjectManager();

    $objectManager->get(Magento\Framework\App\State::class)
        ->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

    /** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
    $customerFactory = $objectManager->create(\Magento\Customer\Api\Data\CustomerInterfaceFactory::class);
    $customer = $customerFactory->create();
    $customer
        ->setEmail('justincase@test123.xyz')
        ->setFirstname('Justin')
        ->setLastname('Case');

    /** @var \Magento\Customer\Api\AccountManagementInterface $accountManager */
    $accountManager = $objectManager->create(\Magento\Customer\Api\AccountManagementInterface::class);
    $accountManager->createAccount($customer);

} catch (Exception $e) {
    echo $e->getMessage();
}

Ok, since I was curious, I quickly (lol) installed magento2 myself. 好的,因为我很好奇,所以我很快就自己安装了magento2。 With the above example I was able to create a customer on a fresh magento2 install. 通过上面的示例,我能够在全新的magento2安装上创建客户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM