Connecting to Dynamics NAV Web Services Using PHP

by | Updated January 7, 2021 | Dynamics NAV

Prerequisites

In order to make Dynamics NAV Web Services work with PHP you need to make sure that SOAP and CURL extensions are installed and enabled in php.ini.

You can check this by running the info.php and checking the following entries:

Once you have verified that the php.ini file is correct you will need to update the Service Tier to use NTLM Authentication.

Note: Be sure to stop and start the service instance to apply the NTLM Authentication change.

Extract The Attached Zip File And Transfer The Contents To The PHP Site.

You will need to update the NTLMUserID.php file to reflect your Username and Password.

After you update the Web Services Credentials you must configure the Web Service URL for PHP Examples.

First, verify that the Web Services instances is running.

Example:

http://abs-dev-nav2016.westus.cloudapp.azure.com:7047/NAV/WS/Services

Next update Examples.php variables to reflect your Web Service instance.

$baseURL = ‘http://abs-dev-nav2016.westus.cloudapp.azure.com:7047/NAV/WS/’;

Note: Be sure to include Port Number and Service Tier name in URL.

Define the company name that you will be connecting to.

$CompanyName = “CRONUS International Ltd.”;

Save changes to the examples.php file.

Run the PHP Examples page.

Read Single Record

$pageURL = $baseURL.rawurlencode($CompanyName).’/Page/Item’;

// Read Single Record

try{

$service = new NTLMSoapClient($pageURL);

$params = array(‘No’ => ‘*1*’);

$result = $service->Read($params);

$item = $result->Item;

echo “Read Single Record Result: Item Filter No = *1*:” . $item->No . “&nbsp;” . $item->Description.”<br><br>”;

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

Read Multiple Records

try{

$params = array(‘filter’ => array( array(‘Field’ => ‘No’,

‘Criteria’ => ‘*1*’)

),

‘setSize’ => 20); //setSize =0 will return all rows – Can cause performance issue with large results set!

$result = $service->ReadMultiple($params);

$resultSet = $result->ReadMultiple_Result->Item;

echo “Read Multiple Records Result: First 20 Items that Contain *1* :<br>”;

if (is_array($resultSet)) {

foreach($resultSet as $rec) {

echo $rec->No . “&nbsp;” . $rec->Description.”<br>”;

}

}

else {

echo $resultSet->No . “&nbsp;” . $resultSet->Description.”<br>”;

}

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

}

Create Record

try {

$create = new stdClass();

//Define The Primary Key Values

$Item_PK = new stdClass();

$Item_PK->No= “NEWITEM” . DATE(his);

//Create The Record

$create->Item = $Item_PK;

$result = $service->create($create);

//Key is the Unique Identifer that is required to perform any additional updates to the data.

$key = $result->Item->Key;

echo “Record Created: Key Value = ” . $key.”<br>Item No.=” . $result->Item->No . “<br>”;

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

}

Update Record

//Note: Key Value is required to update data!

try {

$Update_Rec = new stdClass();

$Update_Rec->Key = $key;

$Update_Rec->No =”UPDATEDITEM” . DATE(his); //Note: We Are Changeing the Primary Key(The Primary Key value is not required, I just wanted to show the Key Value Increments when the Primary Key is changed.)

$Update_Rec->Description = ‘Created by Web Service’; //Text Field

$Update_Rec->Unit_Price = 19.99; //Decimal Field

$Update_Rec->Replenishment_System = ‘Assembly’; //Option Field

$Update_Rec->Blocked = True; //Boolean Field

$Update_Rec->Safety_Lead_Time = ’30D’; //DateFormula Field

$update->Item =$Update_Rec;

$result = $service->Update($update);

$key = $result->Item->Key;

echo “Record Updated: New Key Value = ” . $key.”<br>Item No.=” . $result->Item->No . “<br>”;

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

}

Delete Record

//Note: Key Value is required to delete data!

try {

$Delete_Rec = new stdClass();

$Delete_Rec->Key = $key;

$result = $service->Delete($Delete_Rec);

echo “Record Deleted: Key Value = ” . $key.”<br>”;

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

}

Execute Codeunit Function

try {

$codeunitURL = $baseURL.rawurlencode($CompanyName).’/Codeunit/LibraryRandom’;

echo “<br>Execute NAV Codeunit Call: $codeunitURL<br><br>”;

//Initialize Page Soap Client

$codeunit = new NTLMSoapClient($codeunitURL);

$functionparams = array(‘delta’ => rand(1,10)); //Set Parameters for function

$result = $codeunit->RandDate($functionparams); //Call Codeunit Function To Return Random Date Based Upon delta passed

echo “NAV Codeunit LibraryRandom Result: ” . $result->return_value.”<br>”;

}

catch (Exception $e) {

echo “<hr><b>ERROR: SoapException:</b> [“.$e.”]<hr>”;

echo “<pre>”.htmlentities(print_r($service->__getLastRequest(),1)).”</pre>”;

}

Related Posts

0 Comments

Submit a Comment

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

Upcoming Events

september

01sep10:00 am10:30 amEnsuring Quality and Compliance for Batch Manufacturers in Life Sciences

01sep12:00 pm12:30 pmIs it Worth It to Move to the Cloud? A Look at Considerations for Current Agribusinesses Using Dynamics GP

01sep2:00 pm2:45 pmWhat’s New in Dynamics 365 Finance and Supply Chain

09sep11:00 am12:00 pmConfab Live with Stoneridge – Data Strategy and Reporting – Mining Decision Making Insights

15sep10:00 am11:00 amSolving the Biggest Challenges in Agribusiness Through Innovation and Technology

22sep12:00 am12:30 pmSimplifying Payroll and HR Management with ADP Workforce Now

22sep10:00 am10:30 amStreamlining Batch Manufacturing with Technology

22sep2:00 pm2:30 pmProcess Automation for Microsoft Dynamics D365 for Business Central, Finance and Operations and GP

23sep11:00 am12:00 pmConfab LIVE with Stoneridge - Riding the Wave 2 Release – Key Features Coming to Dynamics 365 this October

29sep10:00 am11:00 amTop Five Reasons Why NOW is the Right Time to Move from Salesforce to Dynamics 365 Customer Engagement

29sep12:00 pm12:45 pmUnderstanding Job Costing and Tax Management in Business Central

29sep2:00 pm3:00 pmDigitalizing Horticulture & Agriculture - How to Sell Plants Online and Simplify Business Management

30sep12:00 pm4:00 pmSecurity and Permissions Training for D365 Business Central or Dynamics NAV

october

06oct10:00 am10:30 amPreview of D365 Business Central Fall Release Features and Functionality

06oct12:00 pm12:30 pmInsider's Guide to New Features Available in the Fall Release of D365 Finance and Supply Chain

07oct11:00 am12:00 pmConfab LIVE with Stoneridge - Dataverse and Dynamics in Review – Let’s Get Technical

21oct11:00 am12:00 pmConfab LIVE with Stoneridge - Dataverse and Dynamics in Review – Let’s Get Functional

26oct(oct 26)9:00 am28(oct 28)5:00 pmStoneridge Connect Leadership and Community Conference

About Stoneridge
Stoneridge Software is a unique Microsoft Gold Partner, with emphasis on partner. With specialties in Microsoft Dynamics 365, Microsoft Dynamics AX, Microsoft Dynamics NAV, Microsoft Dynamics GP and Microsoft Dynamics CRM, we focus on attracting the most knowledgeable experts in the field to our team, and prioritize delivering stellar solutions with maximum impact for your business. At Stoneridge, we are deeply committed to your results. Each engagement is met with a dedicated team, ready to provide thorough, tailored, and expert service. Based in Minnesota, we intentionally “step into your shoes,” wherever you are. We focus on what you care about, and develop trusting, long-term relationships with our clients.

Subscribe To Our Blog

Sign up to get periodic updates on the latest posts.

Thank you for subscribing!

X