Magento 2 从根文件访问 Rest API 接口
Magento 2 access Rest API interface from root file
我在根目录下创建了一个脚本文件,我想从该文件创建一个新客户,下面是我的代码。
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);
但是当我 运行 这段代码时,它给我以下错误。
致命错误:未捕获类型错误:传递给 Magento\Customer\Model\AccountManagement\Interceptor::createAccount() 的参数 1 必须是 Magento\Customer\Api\Data\CustomerInterface 的实例,给定数组,调用在 C:\wamp64\www\mg\m2\rest\v3\Customer.php 第 82 行,在 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(数组)
1 C:\wamp64\www\mg\m2\rest\v3\api.php(7): require_once('C:\wamp64\www\m...')
2 {主要}
投入
C:\wamp64\www\mg\m2\generated\code\Magento\Customer\Model\AccountManagement\Interceptor.php 上线
124
请帮忙。实际上我想直接从代码访问 web api 方法并获得响应,以便我可以相应地修改该响应。因为我们已经在 magento 1.9 中有了应用 运行ning。所以我们不想改变响应
正如错误信息所说。您必须将 Magento\Customer\Api\Data\CustomerInterface
的实现传递给 createAccount
方法。
因此,与其传递像 $customerData
这样的简单数组,不如创建一个 CustomerInterface 实现的新实例...并用所需数据填充它。
通过他们的 github 回购搜索我发现了这个:
Magento\Customer\Model\Data\Customer
https://github.com/magento/magento2/search?utf8=%E2%9C%93&q=%22implements+Magento%5CCustomer%5CApi%5CData%5CCustomerInterface%22&type=
因此,除非您想创建自己的实现,否则这就是您应该传递给 createAccount
的内容
您应该可以像这样通过工厂创建一个:
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();
}
好吧,既然好奇,我赶紧(笑)自己安装了magento2。通过上面的示例,我能够在新安装的 magento2 上创建一个客户。
我在根目录下创建了一个脚本文件,我想从该文件创建一个新客户,下面是我的代码。
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);
但是当我 运行 这段代码时,它给我以下错误。
致命错误:未捕获类型错误:传递给 Magento\Customer\Model\AccountManagement\Interceptor::createAccount() 的参数 1 必须是 Magento\Customer\Api\Data\CustomerInterface 的实例,给定数组,调用在 C:\wamp64\www\mg\m2\rest\v3\Customer.php 第 82 行,在 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(数组)
1 C:\wamp64\www\mg\m2\rest\v3\api.php(7): require_once('C:\wamp64\www\m...')
2 {主要}
投入 C:\wamp64\www\mg\m2\generated\code\Magento\Customer\Model\AccountManagement\Interceptor.php 上线 124
请帮忙。实际上我想直接从代码访问 web api 方法并获得响应,以便我可以相应地修改该响应。因为我们已经在 magento 1.9 中有了应用 运行ning。所以我们不想改变响应
正如错误信息所说。您必须将 Magento\Customer\Api\Data\CustomerInterface
的实现传递给 createAccount
方法。
因此,与其传递像 $customerData
这样的简单数组,不如创建一个 CustomerInterface 实现的新实例...并用所需数据填充它。
通过他们的 github 回购搜索我发现了这个:
Magento\Customer\Model\Data\Customer
https://github.com/magento/magento2/search?utf8=%E2%9C%93&q=%22implements+Magento%5CCustomer%5CApi%5CData%5CCustomerInterface%22&type=
因此,除非您想创建自己的实现,否则这就是您应该传递给 createAccount
您应该可以像这样通过工厂创建一个:
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();
}
好吧,既然好奇,我赶紧(笑)自己安装了magento2。通过上面的示例,我能够在新安装的 magento2 上创建一个客户。