Reputation: 105
I wanted to consume Navision 2016 soap web services from either PHP or jquery. I have tried this solution PHP with Dynamics NAV webservices
I have the following code for soap PHP:
<?php
if (extension_loaded('soap'))
{
// Request parameters :
// Exemple is a Nav Code unit GetSalesPrices, method GetPrice(CodPCustomerNo, CodPItemNo)
$NavUsername = "superUser";
$NavAccessKey = "passworddrowssap";
$CodeunitMethod = "CallMethod";
$params = array(
"employeeNo" => "CUSTOMER_1",
"leaveType" => "ITEM_1",
);
// SOAP request header
$url = "http://DESKTOP-H5GFAKH:7047/DynamicsNAV100/WS/MyCompany/Codeunit/webportals";
$options = array(
'authentication' => SOAP_AUTHENTICATION_BASIC,
'login' => $NavUsername,
'password' => $NavAccessKey,
'trace' => 1,
'exception' => 0,
);
try
{
$client = new SoapClient(trim($url), $options);
$soap_response = $client->__soapCall($CodeunitMethod, array('parameters' => $params));
echo "SOAP REQUEST SUCESS :";
var_dump($soap_response);
}
catch (SoapFault $soapFault)
{
echo "SOAP REQUEST FAILED :<br>";
var_dump($soapFault);
echo "Request :<br>" . htmlentities($soap_client->__getLastRequest()) . "<br>";
echo "Response :<br>" . htmlentities($soap_client->__getLastResponse()) . "<br>";
}
}
else
{
echo "Php SOAP extention is not available. Please enable/install it to handle SOAP communication.";
}
?>
With this I get:
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://DESKTOP-H5GFAKH:7047/DynamicsNAV100/WS/MyCompany/Codeunit/webportals' : failed to load external entity
Upvotes: 2
Views: 3457
Reputation: 755
For starters, you can use POSTMAN and you can get the code that you should use. Having said that, see my code below. You don't have to use SoapClient, just use cURL in PHP. Make sure to use Basic Auth authentication in Navision. The below example is of a request to NAV to get a data from a NAV list
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://URL:PORT/WebService/WS/COMPANY%20NAME/Page/WebService",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS =>"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n<Body>\n<Read xmlns=\"urn:microsoft-dynamics-schemas/page/WebService\">\n<No>" .$entry_no. "</No>\n</Read>\n</Body>\n</Envelope>",
CURLOPT_HTTPHEADER => array(
"Content-Type: text/xml; charset=utf-8",
"SoapAction: urn:microsoft-dynamics-schemas/page/WebService",
"Authorization: Basic " .base64_encode('username:password'). ""
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$xml = simplexml_load_string($response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xmlns', 'urn:microsoft-dynamics-schemas/page/webservice');
$xmlinfo = $xml->xpath('Soap:Body');
User Wizdler from Chrome Store or Firefox extensions to enable you parse the WSDL and generate the SOAP messages.
For jQuery, you can use POSTMAN to generate the required code or you should consider using AJAX
Upvotes: 4