模拟提交http_post_data发送数据的获取方式
我们用如下方法在后端模拟提交数据,那么数据提交后如何获取处理呢?
function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string)) ); ob_start(); $ret = curl_exec($ch); if (curl_errno($ch)) { // $this->ErrorLogger('curl post falied. Error Info: '.curl_error($ch)); } $return_content = ob_get_contents(); ob_end_clean(); return $return_content; }
数据提交
$data = array('username' => 'nihao', 'password' => 'nihao') ); $rs = http_post_data($url, json_encode($data));
数据获取
模拟提交后,后端数据获取方法
$data = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
或者
$postdata = file_get_contents("php://input"); //json $data = json_decode($postdata, true);
这样 $data 就是数组了。