Web services 使用Microsoft Dynamics NAV 2016从PHP创建新实体;s Odata web服务

Web services 使用Microsoft Dynamics NAV 2016从PHP创建新实体;s Odata web服务,web-services,odata,navision,dynamics-nav,dynamics-nav-2016,Web Services,Odata,Navision,Dynamics Nav,Dynamics Nav 2016,作为集成项目的一部分,我需要一个PHP网站,以便能够读取和写入Microsoft Dynamics NAV 2016的Odata服务 我发现从PHP获取现有客户的列表非常简单: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://<Server>:<WebServicePort>/<ServerInstance>/OData/Company(\'<CompanyName>\')/c

作为集成项目的一部分,我需要一个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);
$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);
不幸的是,文档也不是很有用


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

在涉过无数资源并把头撞在墙上之后,我终于成功地创建了一个新客户

我犯了两个错误:

  • 我为我的web服务使用了错误的数据源:我使用了对象ID 22(
    客户列表
    ),而不是对象ID 21(
    客户卡
  • POST数据必须是Json编码的。它不应该是数组或查询字符串。因此,替换
    curl_setopt($ch,CURLOPT_POSTFIELDS,[…])带有
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode[…])成功了

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

嘿,您的第一个问题是客户列表对象不可编辑。我建议创建轻量级页面对象来使用。只需基于自定义区域(50000)中的Customer表创建一个新页面,并添加所有要访问的字段。您不会遇到任何干扰问题,因为自定义对象不会显示给最终用户。为什么要进行向下投票?!
$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);
{
    "odata.error": {
        "code": "",
        "message": {
            "lang": "en-US",
            "value": "An error occurred while processing this request."
        }
    }
}