Business Central consumption of Web Services from PHP
In this post Part1 we will talk about how to connect and consuming the Web Services exposed by Business Central from PHP, in this case to read and modify data using both SOAP and OData Services.
We will use XAMPP Suite which can also be used on Windows to simulate a complete environment for PHP, we will see how it is possible to connect, read and modify data via PHP.
In my blog we had already seen other examples related to the consumption of WS with Python, C #, VMC and Xamarin, only the use with PHP was missing!. In Part 2 of the post, we will talk about how to use Web Services to insert \ modify data.
PHP – Windows Environments
We will use everything on Windows via XAMPP, a complete multi-environment distribution with:
- and “Visual Studio Code” as Editor! Yes!
Installing XAMPP Environment
RUNNING XAMPP Services
- In this scenario you will need Apache, MySql (and PHP)
PHP “SOAP” in php.ini
In order to make Business Central Web Services work with PHP you need to make sure that SOAP and CURL extensions are installed and enabled in php.ini file.
Open php.ini file
Check lines and toggle “;” comment if active on:
- SOAP > ENABLED
- cURL Support > ENABLED
Create a New Project
Test that the XAMPP Servers and the project are visible on the web page:
- You can use Vs Code to create and edit your PHP Project!
- Config the Project path (url)
A very short Example to start
You are ready to work!… and now with some real examples…
PHP – Example Scenario
The example allows you to connect an application written in PHP on Apache server with a Business Central SaaS instance.
The example creates a list of customers and allows editing of the fields and subsequent saving on BC SaaS.
Customer List Page (on BC)
Customer List (on My Web App) … different look but same data..
The customer record “10000” is changed in his company name on the web application, and the change is recorded (by Posting Button)
After posting the change on the PHP side, we consult the “Customer List” on BC and we find that the company name of the customer “10000” has been consistently changed.
HOW DOES IT WORK
From the code point of view, access to BC from PHP is made using the CURL interface. The connection parameters are stored in an array.
The call to the function that extracts the data is made by parameterizing the Odata service to be called, which corresponds to what is published on BC via the “Web Services” page.
The data is actually read by the function that makes the connection through the class “php Curl”
Following the modification / registration operation, the data is stored on BC by calling the corresponding WSDL service, always exposed by the publication of the Customers page through the Web Services.
As anticipated at the beginning of the post, there are several tools that can be used to achieve the same result.
Using Guzzle HttpClient
- From XAMPP install the components
Define “to Require” guzzle > in composer.json file
Use Guzzle HTTP Elements (Client, Entity Body and Message classes) in your PHP Project
OData 4.0 Call
URL must be defined (for OData), authentication method, user and password or ServiceKey.
Check your credentials and activate NTLM if you can’t log in, check password and Service Key.
Example of incorrect login (with password blank in this case)
Standard Demo for Guzzle > read your Github user