【问题标题】:Laravel + Magento - Retrieve list of all countries from MagentoLaravel + Magento - 从 Magento 检索所有国家/地区的列表
【发布时间】:2018-09-15 00:37:25
【问题描述】:

在我的应用程序中,它需要拥有来自 Magento 的所有国家/地区的列表。现在我在 Magento DevDocs 中找到了一个包含 Country API 的链接。但是,如何通过使用 foreach 来实现应用程序中所有国家/地区的列表?我需要该列表,以便可以将选定的值插入到我的数据库中。这是我找到的链接:Link to the docs

这是需要显示的表单:

<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>

【问题讨论】:

  • 你能 var_dump $countries 吗?
  • 这就是我想要的工作。目前变量 country 是空的,因为我还没有实现 API。这就是问题所在。我不知道如何实现API并获取所有国家。变量需要填写API中的国家
  • 这很难看,但是如果您无法记录 $countries 数组,请尝试使用 echo json_encode($countries); 输出它。这应该可以帮助您弄清楚数组的结构。

标签: php laravel api magento laravel-5


【解决方案1】:

要将数据从 Magento 检索到另一个应用程序,您可以使用 XML-RPC、SOAP 或 REST。

我不会做太多的 PHP,但我在 Python 中做过类似的事情。我使用了 XMLRPC

在 PHP 中应该有一个库来执行此操作。在文档中,他们提到了 Zend 的 lib。刚刚找到this,看起来你可能想看看。

要使用 API,您必须获得凭据和访问权限。在 Magento 中,转到 System &gt; Web Services &gt; SOAP/XMLRPC Users|Roles 并创建一个用户,授予用户一些访问国家 API 的权限。

然后在您的 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')); 

然后通过

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

从这里你的$countries 变量应该有来自 Magento 的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多