dongshao8125 2016-02-08 09:02
浏览 38

too long

I am trying to send JSON to a java web service but getting response from web service that paramaters all null see below. Is there anything wrong with my code?

$buildApplication = array(
    'firsname'          => 'Keith',
    'surname'           => 'Francis',
    'companyName'       => 'Keiths Mobile Discos',
    'phone'             => '07123456789',
    'email'             => 'keith.francis@freedom-finance.co.uk',
    'sourceCode'        => 'W00T'
);
$data = json_encode($buildApplication);                                                                                                             
$ch = curl_init('http://10.50.1.71:8080/SME/api/details.json');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));                                                                 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($data))                                                                       
);                                                                                                                
$result = curl_exec($ch);
var_dump($result);

Response comes back

string(1042) "{"errors":[{"object":"com.application.AppDetails","field":"firstname","rejected-value":null,"message":"Property [firstname] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"surname","rejected-value":null,"message":"Property [surname] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"companyName","rejected-value":null,"message":"Property [companyName] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"phone","rejected-value":null,"message":"Property [phone] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"email","rejected-value":null,"message":"Property [email] of class [class com.application.AppDetails] cannot be null"},{"object":"com.application.AppDetails","field":"sourceCode","rejected-value":null,"message":"Property [sourceCode] of class [class com.application.AppDetails] cannot be null"}]}"
  • 写回答

2条回答 默认 最新

  • dongmeba4877 2016-02-08 09:06
    关注
    • You have posted firsname instead of firstname.
    • Also you used json_encode twice.

    Try as below :

    $buildApplication = array(
        'firstname'          => 'Keith',
        'surname'           => 'Francis',
        'companyName'       => 'Keiths Mobile Discos',
        'phone'             => '07123456789',
        'email'             => 'keith.francis@freedom-finance.co.uk',
        'sourceCode'        => 'W00T'
    );
    $data = json_encode($buildApplication);                                                                                                             
    $ch = curl_init('http://10.50.1.71:8080/SME/api/details.json');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);                                                                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data))                                                                       
    );                                                                                                                
    $result = curl_exec($ch);
    var_dump($result);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM