JSON POST请求适用于java而不是PHP?

时间:2017-11-14 09:32:23

标签: java php curl

我有一个奇怪的问题,我似乎根本无法解决。我们有一个休息服务,它应该接受提交数据作为JSON对象。

我一直在使用卷曲圆圈。该命令适用于公司网络以外的任何服务,以及任何使用curl代理选项的EXTERNAL服务,但内部网络上无法访问(获取或发布)任何服务。我总是得到一个" false"作为回应回来。这是PHP代码:

cat /etc/cron.d/raid-check
Run system wide raid-check once a week on Sunday at 1am by default
0 1 * * Sun root /usr/sbin/raid-check

及其输出:

$data_string =  "{\"fashionSignups\":[{\"corporateBrandId\":0,\"vendorId\":null,\"countryCode\":\"pt\",\"languageCode\":\"pt\",\"email\":\"test@ytDessddefwwt.com\",\"genderCode\":0,\"hasChildren\":0,\"zipcode\":\"12345\"}]}";
$ch = curl_init('http://xxx.xxx.com/fashionnewssignup');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
//curl_setopt($ch, CURLOPT_PROXY, "http://proxy.xxxx.com");
//curl_setopt($ch, CURLOPT_PROXYPORT, "8080");
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, CRED);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 50);

//execute post
$result = curl_exec($ch);
echo "<pre>";
var_dump( curl_getinfo($ch) ) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
//close connection
curl_close($ch);
var_dump ($result);
die;

&#34;来自服务器&#34;的空回复52是我得到的,无论我输入什么URL。如上所述,这个代码可以在网络上与其他网站合作。

git-bash curl给出了这个:

array(26) {
  'url' =>
  string(59) "http://xxx.xxx.com/fashionnewssignup"
  'content_type' =>
  NULL
  'http_code' =>
  int(0)
  'header_size' =>
  int(0)
  'request_size' =>
  int(251)
  'filetime' =>
  int(-1)
  'ssl_verify_result' =>
  int(0)
  'redirect_count' =>
  int(0)
  'total_time' =>
  double(0.031)
  'namelookup_time' =>
  double(0)
  'connect_time' =>
  double(0.016)
  'pretransfer_time' =>
  double(0.016)
  'size_upload' =>
  double(0)
  'size_download' =>
  double(0)
  'speed_download' =>
  double(0)
  'speed_upload' =>
  double(0)
  'download_content_length' =>
  double(-1)
  'upload_content_length' =>
  double(-1)
  'starttransfer_time' =>
  double(0.031)
  'redirect_time' =>
  double(0)
  'redirect_url' =>
  string(0) ""
  'primary_ip' =>
  string(12) "XX.XX.XXX.12"
  'certinfo' =>
  array(0) {
  }
  'primary_port' =>
  int(1080)
  'local_ip' =>
  string(13) "XX.XX.XXX.196"
  'local_port' =>
  int(56107)
}
52<br/>Empty reply from server<br/>C:\xampp\htdocs\at\test.php:46:
bool(false)

Process finished with exit code 0

奇怪的是,当我使用此代码在同一网络上的同一台机器上使用JAVA尝试同样的事情时,它可以100%正常工作,无代理或任何需要。

这个JAVA代码有效:

    curl -H "Content-Type: application/json" -X POST -d '{"fashionNewsSignups":[{"                                    corporateBrandId":0,"vendorId":null,"countryCode":"pt","languageCode":"pt","emai                                    l":"test@ytDessddefwwt.com","genderCode":0,"hasChildren":0,"zipcode":"12345"}]}'                                     http://xxx.xxx.com/fashionnewssignup
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   184    0     0  100   184      0   5935 --:--:-- --:--:-- --:--:-- 12266 
curl: (52) Empty reply from server

任何人都知道我做错了什么?我想要访问的服务是用JAVA编写的,如果这很重要:/

2 个答案:

答案 0 :(得分:0)

$data_string =  '{"fashionSignups":[{"corporateBrandId":0,"vendorId":null,"countryCode":"pt","languageCode":"pt","email":"test@ytDessddefwwt.com","genderCode":0,"hasChildren":0,"zipcode":"12345"}]}';

$data_string = json_decode($data_string,true);

$ data_string是您的案例中的字符串,而您的内容类型是JSON 所以上面的代码会将你的字符串转换为JSON对象,它应该可以解决你的问题。

答案 1 :(得分:0)

好的,最后解决了,需要这条线:

curl_setopt($ch, CURLOPT_NOPROXY , 'xxx.xxx.com');

任何有相同问题的人:)

我没有尝试过,因为它不在curl_d.php的常量列表中,而intelliJ报告“常量未定义” ......但它有效吗?!? :○ - 谁知道哈哈。