简体   繁体   English

Laravel + Magento - 从 Magento 中检索所有国家的列表

[英]Laravel + Magento - Retrieve list of all countries from Magento

In my application it is required to have a list of all the countries from Magento. Now I found a link in the Magento DevDocs which has a Country API. But how can I implement the list of all the countries in my Application by using for, instance, a foreach?在我的应用程序中,它需要有一个来自 Magento 的所有国家/地区的列表。现在我在 Magento DevDocs 中找到了一个链接,其中有一个国家/地区 API。但是我如何通过使用 for 在我的应用程序中实现所有国家/地区的列表,例如,一个foreach? I need the list so I can insert the selected value into my database.我需要这个列表,这样我就可以将选定的值插入到我的数据库中。 This is the link that I found: Link to the docs这是我找到的链接: Link to the docs

This is the form where It needs to be displayed:这是需要显示的形式:

<div class="form-group">
 <span class="andcode-dropdown">
  <label for="country" class="label-muted">Land <span class="required">* 
  </span></label>
  <select class="form-control andcode-select" name="country_name" id="country">
   <option selected>Kies een land...</option>
   @foreach($countries as $country)
   <option>{{ $country->country_name }}</option>
   @endforeach
  </select>
 </span>
</div>

To retrieve data from Magento to another app, you can use XML-RPC, or SOAP or REST. 要将数据从Magento检索到另一个应用程序,可以使用XML-RPC或SOAP或REST。

I don't do much of PHP but I've done the similar thing in Python. 我没有做太多的PHP,但是我在Python中做过类似的事情。 And I used XMLRPC . 我使用了XMLRPC

In PHP there should be a library to do so. 在PHP中应该有一个库来这样做。 In the doc, they mentioned about Zend's lib. 在文档中,他们提到了Zend的lib。 Just found this , looks like what you might want to take a look. 刚发现这个 ,看起来像您可能想看看。

To use the API, you have to acquire credentials and access rights. 要使用该API,您必须获取凭据和访问权限。 In Magento, go to System > Web Services > SOAP/XMLRPC Users|Roles and create a user, grant the user some privileges to access to the country API. 在Magento中,转到“ System > Web Services > SOAP/XMLRPC Users|Roles并创建一个用户,向该用户授予一些访问国家API的特权。

Then on your PHP code, set the credentials on the connection. 然后在您的PHP代码上,在连接上设置凭据。

$client = new Zend_XmlRpc_Client('http://magentohost/api/xmlrpc/');

// If somestuff requires api authentification,
// we should get session token
$session = $client->call('login', array('apiUser', 'apiKey')); 

Then retrieve the list by 然后通过检索列表

$countries = $client->call('call', array($session, 'directory_country.list'));

From this your $countries variable should have the data from Magento. 由此,您的$countries变量应具有Magento的数据。

First, since you got the empty list then I suspect that you are not authorizing the request before call, to use RestAPI in Magento you will have to create an Integration record to generate the API key to access Magento core APIs.首先,由于您得到的是空列表,因此我怀疑您在调用之前未授权请求,要在 Magento 中使用 RestAPI,您必须创建一个集成记录以生成 API 密钥以访问 Magento 核心 API。

For reference please follow this link: https://www.beehexa.com/devdocs/how-to-create-an-integration-in-magento-2如需参考,请点击此链接: https://www.beehexa.com/devdocs/how-to-create-an-integration-in-magento-2

When you have the API key in hand, put it in your header authorization and try again to fetch the API you need (Country API) in this case.当您手头有 API 密钥时,将其放入您的 header 授权中,然后再次尝试获取您需要的 API(在本例中为国家/地区 API)。

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

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