如果你想处理JAVA过来的接口。在PHP里面是这样表现的:
<?php $url = ‘此处为JAVA提供的接口URL地址‘; //我们统一用的是JSON格式 $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $contents = curl_exec($ch); curl_close($ch); $body = json_decode ($contents, TRUE); $data = array(); if ( $body[‘code‘] == 200 ) { $data = (array)$body[‘data‘]; } ?>
下面是处理好了返回给javascript的JSON字符串。你只需要把控制器的地址用URL的形式给对接人员就可以了。
<?php public function action_ActionName() { header(‘Content-Type: application/x-javascript‘); // 例如只接收.baidu.com域名的请求 header(‘Access-Control-Allow-Origin:.baidu.com‘); $callback = isset( $_GET[ ‘callback‘ ] ) ? $_GET[ ‘callback‘ ] : ‘callback‘; try { $rows = select * from *******; //此处为SELECT语句,从数据库取出结果 $data = array( ‘code‘ => 200, ‘msg‘ => ‘成功‘, ‘data‘ => $rows, ); } catch (Exception $e) { $data = array( ‘code‘ => 3000004, ‘msg‘ => ‘非法的操作‘, ); } echo $callback.‘(‘.json_encode($data).‘)‘; exit; } ?>