简体   繁体   English

使用Microsoft Dynamics NAV 2016的Odata Web服务从PHP创建新实体

[英]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. 作为集成项目的一部分,我需要一个PHP网站,以便能够读取和写入Microsoft Dynamics NAV 2016的Odata服务。

I found that fetching a list of existing customers from PHP is as simple as this : 我发现从PHP获取现有客户列表就像这样简单:

$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 : 我还发现从PHP中获取单个客户就像这样简单:

$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 : 无论我设置为POST字段,我都会收到这个完全无用的错误消息:

{
    "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 ). 我为我的Web服务使用了错误的数据源:我使用了对象ID 22( Customer List )而不是对象ID 21( Customer Card )。
  • The POST data has to be Json-encoded. POST数据必须是Json编码的。 It should not be an array or query string. 它不应该是数组或查询字符串。 So, replacing curl_setopt($ch, CURLOPT_POSTFIELDS, [...]); 所以,替换curl_setopt($ch, CURLOPT_POSTFIELDS, [...]); with curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode[...])); with curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode[...])); did the trick. 做了伎俩。

I hope this information will help others save time. 我希望这些信息能帮助他人节省时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Microsoft Dynamics NAV 2016 中使用 Odata Web 服务获取特定集合或实体的 $metadata - Fetching $metadata for a specific collection or entity with Odata web services in Microsoft Dynamics NAV 2016 使用Web服务的Microsoft Dynamics Nav Post - Microsoft Dynamics Nav Post using Web Services 通过Web服务或API在Microsoft Dynamics导航中创建订单 - Creating Orders in Microsoft Dynamics NAV via web services or an API Microsoft Dynamics Nav远程访问Web服务 - Microsoft Dynamics Nav access web services remotely ASP.NET MVC + Dynamics导航odata Web服务-如何访问相关模型? - ASP.NET MVC + Dynamics NAV odata web services - how do I access related models? 是否可以在 Dynamics Nav 2016 中通过 ODATA 查询所有公司 - Is it possible to query all companies via ODATA in Dynamics Nav 2016 从Android连接到Ms Dynamics导航Web服务 - Connecting to Ms dynamics nav web services from android 如何使用Dynamics NAV Web服务在Java中插入Line table? - How to insert Line table in Java using Dynamics NAV web services? 以Microsoft JSON格式查询Microsoft Dynamics NAV 2013 Odata服务 - Querying Microsoft Dynamics NAV 2013 Odata service as JSON Format 什么是Web服务的Dynamics NAV许可要求? - What are Dynamics NAV licensing requirements for Web Services?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM