尝试使用 curl 函数从 php 客户端调用 java 服务。我的 api 期待一个像下面这样的 json 请求正文,
{"personRequest":{"emailId":"rupanjan@gmail.com","isActive":"true"}}
而我的php客户端代码如下,
$postData['personRequest'] = array(
'emailId' => $emailId,
'isActive' => "true"
);
//$data = Array();
//$data['personRequest']['isActive'] = "true";
//$postFields = json_encode($data);
$postFields = json_encode($postData);
$url = "$server_url" . "service/membership/update";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postFields))
);
$result = curl_exec($ch);
var_dump(curl_getinfo($ch));
curl_close($ch);
echo $ch;
print_r($result);
执行后它显示一个空白响应,我也可以访问服务器代码,试图在我的 java 代码中放置一个调试点,似乎 http 调用根本没有到达我的服务器..
刚才执行了 var_dump(curl_getinfo($ch)); 得到以下输出,array(22) { ["url"]=> string(66) " http://9splatform.com/sample-server/service/membership/update " ["content_type"]=> NULL ["http_code "]=> int(302) ["header_size"]=> int(306) ["request_size"]=> int(215) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int (0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.004441) ["namelookup_time"]=> float(0.000245) ["connect_time"]=> float(0.000705) ["pretransfer_time "]=> 浮动(0.000714) ["size_upload"]=> 浮动(65) ["size_download"]=> 浮动(0) ["speed_download"]=> 浮动(0) ["speed_upload"]=>http://9splatform.com/sample-server/login.html;jsessionid=A32AF520FE766BB5D444823D1419F4M9 “}
这意味着这是重定向到登录页面,任何人都可以帮助我......我不知道为什么会这样......