如何POST一个JSON格式的数据给java接口,获得返回数据


/**
     * 模拟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的接口地址


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM