/** * 模擬post進行url請求 * @param string $url * @param json $post_data */ public function request_post($url = '',$ispost=true, $post_data) { if (empty($url) || empty($post_data)) { return false; } header("Content-type: text/html; charset=utf-8"); $ch = curl_init($url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($post_data)) ); $result = curl_exec($ch); return $result; }
傳遞的$post_data 要通過json_decode傳化為json格式,$url傳遞java的接口地址