如何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