Creating new entities from PHP using the Microsoft Dynamics NAV 2016's Odata web services
<p>As part of an integration project, I need a PHP website to be able to both read from and write to Microsoft Dynamics NAV 2016's Odata services.</p>

<p>I found that fetching a list of existing customers from PHP is as simple as this :</p>

<pre><code>$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://&lt;Server&gt;:&lt;WebServicePort&gt;/&lt;ServerInstance&gt;/OData/Company(\'&lt;CompanyName&gt;\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Connection: Keep-Alive',    
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',   
        "Accept: */*"                       
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);

// Close handle
curl_close($ch); 

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);

// Close handle
curl_close($ch);
</code></pre>

<hr>

<p>I also found that fetching a single customer from PHP is as simple as this :</p>

<pre><code>$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://&lt;Server&gt;:&lt;WebServicePort&gt;/&lt;ServerInstance&gt;/OData/Company(\'&lt;CompanyName&gt;\')/customer(\'&lt;Id&gt;\')'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Connection: Keep-Alive',    
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',   
        "Accept: */*"                       
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);

// Close handle
curl_close($ch);
</code></pre>

<hr>

<p>So far, so good. Now, my problem is that I'm struggling to figure out how to create any new customers.</p>

<p>I tried this :</p>

<pre><code>$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://&lt;Server&gt;:&lt;WebServicePort&gt;/&lt;ServerInstance&gt;/OData/Company(\'&lt;Company&gt;\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'Name' =&gt; 'WebServiceTestCustomer',
    'Address' =&gt; 'TestCustomerStreet 55',
    'Credit_Limit_LCY' =&gt; 0
]);
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Connection: Keep-Alive',    
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',   
        "Accept: */*"                       
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);

// Close handle
curl_close($ch);
</code></pre>

<p>That didn't work.</p>

<hr>

<p>As I figured it might be lacking some fields, I also tried this :</p>

<pre><code>$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://&lt;Server&gt;:&lt;WebServicePort&gt;/&lt;ServerInstance&gt;/OData/Company(\'&lt;Company&gt;\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'Name' =&gt; 'WebServiceTestCustomer',
    'Phone_No' =&gt; '016666666',
    'Post_Code' =&gt; '3000',
    'Country_Region_Code' =&gt; 'BE',
    'Currency_Code' =&gt; 'EUR',
    'Language_Code' =&gt; 'NL',
    'Customer_Posting_Group' =&gt; 'BINNENLAND',
    'Gen_Bus_Posting_Group' =&gt; 'BINNENLAND',
    'VAT_Bus_Posting_Group' =&gt; 'BINNENLAND',
    'Payment_Terms_Code' =&gt; '14 DAGEN',
    'Reminder_Terms_Code' =&gt; 'NEDERLANDS'
]);
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');   
curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Connection: Keep-Alive',    
        'Accept: application/json',          
        'Content-Type: application/json; charset=utf-8',   
        "Accept: */*"                       
]);   

$response = json_decode(curl_exec($ch), TRUE);
echo json_encode($response, JSON_PRETTY_PRINT);

// Close handle
curl_close($ch);
</code></pre>

<p>That didn't work either.</p>

<hr>

<p>Regardless of whatever I set as POST fields, I keep getting this totally unhelpful error message :</p>

<pre><code>{
    "odata.error": {
        "code": "",
        "message": {
            "lang": "en-US",
            "value": "An error occurred while processing this request."
        }
    }
}
</code></pre>

<p>Unfortunately, the documentation isn't very helpful either.</p>

<p>Does anyone here have a clue how to fix this?</p>