繁体   English   中英

使用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, [...]); with curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode[...])); 做了伎俩。

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

暂无
暂无

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

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