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

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

作为集成项目的一部分,我需要一个 PHP 网站来读取和写入 Microsoft Dynamics NAV 2016 的 Odata 服务。

我发现从 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);

我还发现从 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);

到目前为止,还不错。现在,我的问题是我正在努力弄清楚如何创建任何新客户。

我试过了:

$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);

那没用。


因为我认为它可能缺少一些字段,所以我也尝试了这个:

$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);

那也没用。


无论我将什么设置为 POST 字段,我都会不断收到完全无用的错误消息:

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

不幸的是,文档也不是很有用。

这里有人知道如何解决这个问题吗?

找了无数的资源,碰壁后,我终于成功创建了一个新客户。

我犯了两个菜鸟错误:

  • 我为我的 Web 服务使用了错误的数据源:我使用了对象 ID 22 (Customer List) 而不是对象 ID 21 (Customer Card)。
  • POST 数据必须进行 Json 编码。它不应是数组或查询字符串。因此,将 curl_setopt($ch, CURLOPT_POSTFIELDS, [...]); 替换为 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode[...])); 就可以了。

我希望这些信息能帮助其他人节省时间。