How to Convert XML APIs & Direct PHP Files

  • Share
  • Print this article

XML APIs allow you to connect from a PHP website and convert the data from the XML format to display on your Web pages. APIs are programming interfaces used by external websites to distribute data to other developers' applications. PHP includes internal libraries that let you connect to any XML API to import the data from the external app.

Instructions

    • 1

      Right-click the PHP page you want to use to connect to the external XML API. Click "Open With" and choose your preferred editor.

    • 2

      Connect to the API. Most APIs require a username and password to connect to the server and perform the requests. The following code connects to an API with a username and password:

      $api = RPC::XML::string->new('/site/api');
      $input = RPC::XML::struct->new({ 'username' => RPC::XML::string->new('XMLroot'),
      'password' => RPC::XML::string->new('password') });
      $client = new SoapClient( 'https://site.com/soap?wsdl' );

      Replace "site.com" with the domain for the XML API. Replace the username and password with your own. If your API doesn't require a username and password, you don't need to use the second statement.

    • 3

      Call the function that returns the data. The function you use is dependent on the API to which you connect. The following code calls the "getCustomers" function that returns a list of customers:

      $customers = $client->call( 'getCustomers');

    • 4

      Display the customers on the page to display them to the reader. The following code loops through each customer value and displays it on the Web page:

      foreach ($customers as $customer)
      {
      echo $customer['name'].'<br>';
      }

Related Searches

References

Comments

Related Ads

Featured
View Mobile Site