Using MS Dynamics NAV with PHP

koppa picture koppa · Jan 16, 2012 · Viewed 9.5k times · Source

I’m starting an extranet project, where php pages are supposed to send data to Microsoft Dynamics NAV.

I haven’t used NAV before, but I found some info here.

The example php code looks pretty clear to me, but is there any tips or tricks (basics) that I should know before starting this project? all examples are welcome…

Answer

John Slegers picture John Slegers · Feb 18, 2016

How to connect with Navision's web services from PHP

Step 1 : Checking your configuration

You need to make sure that NTLM is enabled in the CustomSettings.config file :

<add key="ServicesUseNTLMAuthentication" value="true" />

Step 2 : Choose between OData and SOAP

Since Microsoft Dynamics NAV 2009, Microsoft Dynamics NAV supports OData web services in addition to the SOAP web services. Personally, I find the Odata protocol a lot more intuitive than the SOAP protocol.

OData also has the additional advantage of supporting Json instead of XML for communicating between server and client, which makes conversion from and to standard PHP arrays or objects easy as pie.

See the official MSDN documentation for more info on where to find a list of existing web services (with corresponding URLs) and how to register new ones.


Step 3 : Sending a HTTP request :

If you're going with SOAP, you might want to use PHP's SoapClient or some third party library based on it for sending and receiving SOAP messages.

However, if you know how to parse XML in PHP, you could just use cURL and parse the XML responses yourself. Or, if you've chosen for the Odata protocol, you could use Json messages instead. SOAP is XML only.

Anyway, if you're using cURL, sending a GET request to your SOAP or Odata service can really be as simple as this :

// Create curl handle
$ch = curl_init(); 

// Set HTTP options
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_USERPWD, 'username:password'); 

// Get & output response (= XML or Json string)
echo curl_exec($ch);

// Close handle
curl_close($ch);

Step 3 : Parsing your response :

Parsing a SOAP response can be as simple as this :

 $data = simplexml_load_string(str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response));

Parsing a Json Odata response can be as simple as this :

$data = json_decode($response);