Child pages
  • Chapter 3 - First steps - Access the Web service and list client

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Table of content

Table of Contents
maxLevel2

...

First steps - Access the Web service and list client

...

  1. Configure your PHP installation so that it has the cURL extension installed and activated:
    • With Windows: Place this line in your php.ini file:

      Code Block
      
      extension=php_curl.dll
      
    • Linux/Mac: install the cURL extension:

      Code Block
      sudo apt-get install php5-curl
  2. Copy the PSWebServiceLibrary.php file at the root of your Web server.

    Note

    You can also do this tutorial on a local server even while your shop is on the Internet.

  3. Create a list_the_clients.php file at the root of the Web server that you have chosen.
  4. Specify where to find the web server in your file:

    Code Block
    
    require_once( './ PSWebServiceLibrary.php' );
    

Configured this way, your file should be found in the same folder as PSWebServiceLibrary.php.

...

Accessing the web service

In this section we will see how to access the web service using the PHP library.

...

  • The store's root path (ex: http://myprestashopexample.com/ ).
  • The authentication key (ex: ZR92FNY5UFRERNI3O9Z5QDHWKTP3YIIT).
  • A boolean value, indicating whether the Web service must use its debug mode.

If you do not understand the terms of object-oriented programming such as instance, method, or constructor, that's okay for the rest of the tutorial. Here's how you create a Web service call:

Code Block

$webService = new PrestaShopWebservice('http://myprestashopexample.com/', 'ZR92FNY5UFRERNI3O9Z5QDHWKTP3YIIT', false);

...

The error handling is done within a try..catch block, with the web service processing being done in the try section, the catch one containing the error handling code.

Code Block

try {
  // Execution ( if an error occurs in this code, stops and goes in the catch block)
}
catch {
  // Error handling (tries to catch the error or the error display)
} 

...

  1. In the try block, we instantiate the PrestaShopWebservice object with the necessary parameters and retrieve the customer resource XML in a variable.
  2. In the catch block, we put code to display the PHP error message, if anything wrong happens in the try block.
Code Block

try {
	// creating web service access
	$webService = new PrestaShopWebservice('http://maboutiqueexample.com/', 'ZR92FNY5UFRERNI3O9Z5QDHWKTP3YIIT', false);

	// call to retrieve all clients
	$xml = $webService->get(array('resource' => 'customers'));
}
catch (PrestaShopWebserviceException $ex) {
	// Shows a message related to the error
	echo 'Other error: <br />' . $ex->getMessage(); 
}

...

Key

Value

resource

customers

Code Block

// The key-value array
$opt['resource'] = 'customers';

Retrieving the XML data
$xml = $webService->get($opt); 

...

Launching the code above will return a *impleXML SimpleXML object containing all the client IDs.

Code Block

<?xml>
<prestashop>
  <customers>
    <customer>
      Client ID
    </customer>
    <customer>
      Client ID
    </customer>
    <customer>
      Client ID
    </customer>
...Other client tags
  </customers>
</prestashop>

...

To access the fields of clients who are children from the Customers tag, we only need to retrieve all fields in an associative array in SimpleXML like this:

Code Block

$resources = $xml->customers->children();

From there, we can access client IDs easily. Here's an example with a path from identifiers:

Code Block

foreach ($resources as $resource)
	echo $resource->attributes() . '<br />'; 

Thanks to these elements, we can create a HTML table containing all the client IDs. Try that before reading the next chapter.

You can use the "CcustomersCustomers" menu in the back-office to find the IDs of all customers. If you encounter difficulties, have a look at the example file named 0-CustomersList.php, to see the results you should get.