Consume Nav WebService CodeUnit with Php

You can find plenty information over internet to consume nav web service but lot of them need modification for working in your case. I provide you a Php code to consume Nav Codeunit by SOAP Webservice tested and working.

Nav part :

– Create a codeunit with public method you want to be accessible.
– Publish the code unit with “Web service” page (simply create a new line with the Cu n° and tick “published”)
– Create a specific not windows user in Nav and create him a web service access key
– Define the user permission (you can set it to super if you control the service published)
For the php part you’ll need to provided the SOAP address of the code unit, the user/access key created. You can also provide the code unit methods et parameters to call (this can be retrive in the web service WSDL).

Php part :

You’ll need to have the Soap extention installed on your php server to run following code.
NB : Code unit paramaters are case sensitive this can throw you the “Parameter in method is null” error if they’re not provided exactly same as in the Nav code unit.

Consume Nav CodeUnit Function.php.zip

<?php

if (extension_loaded('soap'))
{
    // Request parameters :
    // Exemple is a Nav Code unit GetSalesPrices, method GetPrice(CodPCustomerNo, CodPItemNo)
    $NavUsername = "WEBSERVICECONSUMING";
    $NavAccessKey = "wGEbboEIdKgEPzE0N0vuuVE/2e/0lzvCsTtB9n9L7KI=";
    $CodeunitMethod = "GetPrice";
    $params = array(
        "CodPCustomerNo" => "CUSTOMER_1",
        "CodPItemNo" => "ITEM_1",
    );

    // SOAP request header
    $url = "http://exemple.net:9012/navservice/WS/cronus/Codeunit/GetSalesPrices";
    
    $options = array(
        'authentication' => SOAP_AUTHENTICATION_BASIC,
        'login' => $NavUsername,
        'password' => $NavAccessKey,
        'trace' => 1,
        'exception' => 0,
    );

    try
    {
        $client = new SoapClient($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.";
}

?>

This code simply show the request result with echo to see if it’s ok, but you can directly handle result value with $soap_response->return_value

2 Comments

  1. Good Morning,

    I try your code but I receive an error like: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://192.168.0.13:7047/NAV/WS/ and so on… Can you help me to hunderstand why it happen? thank you

Leave a Reply

Your email address will not be published. Required fields are marked *