skip to Main Content

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

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>

2

Answers


  1. To retrieve data from Magento to another app, you can use XML-RPC, or SOAP or REST.

    I don’t do much of PHP but I’ve done the similar thing in Python. And I used XMLRPC.

    In PHP there should be a library to do so. In the doc, they mentioned about Zend’s 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. 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.

    Then on your PHP code, set the credentials on the connection.

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

    Login or Signup to reply.
  2. 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.

    For reference please follow this link: 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search