简体   繁体   中英

Creating new entities from PHP using the Microsoft Dynamics NAV 2016's Odata web services

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.

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

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/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);

I also found that fetching a single customer from PHP is as simple as this :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/customer(\'<Id>\')'); 
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);

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

I tried this :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<Company>\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'Name' => 'WebServiceTestCustomer',
    'Address' => 'TestCustomerStreet 55',
    'Credit_Limit_LCY' => 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);

That didn't work.


As I figured it might be lacking some fields, I also tried this :

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<Company>\')/customer'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, [
    'Name' => 'WebServiceTestCustomer',
    'Phone_No' => '016666666',
    'Post_Code' => '3000',
    'Country_Region_Code' => 'BE',
    'Currency_Code' => 'EUR',
    'Language_Code' => 'NL',
    'Customer_Posting_Group' => 'BINNENLAND',
    'Gen_Bus_Posting_Group' => 'BINNENLAND',
    'VAT_Bus_Posting_Group' => 'BINNENLAND',
    'Payment_Terms_Code' => '14 DAGEN',
    'Reminder_Terms_Code' => '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);

That didn't work either.


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

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

Unfortunately, the documentation isn't very helpful either.

Does anyone here have a clue how to fix this?

After wading through countless resources and banging my head against the wall, I finally managed to create an new customer.

I made two noob mistakes :

  • I used the wrong data source for my web service : I used Object ID 22 ( Customer List ) instead of Object ID 21 ( Customer Card ).
  • The POST data has to be Json-encoded. It should not be an array or query string. So, replacing curl_setopt($ch, CURLOPT_POSTFIELDS, [...]); with curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode[...])); did the trick.

I hope this information will help others save time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM