PHP架構調用Java後端,參數傳遞不過去的問題解決

來源:互聯網
上載者:User
本篇文章給大家分享的內容是PHP架構調用Java後端,參數傳遞不過去的問題解決,有著一定的參考價值,有需要的朋友可以參考一下


 public function request($requestURL,$params='',$method ='GET',$contentType='',$user=''){        $timeout = 30;        $ch = null;        if ('POST' === strtoupper($method)) {            $ch = curl_init($requestURL);            curl_setopt($ch, CURLOPT_POST, 1);            curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);            curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);            if (is_string($params)) {                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);            } else {                curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));            }        } else if('GET' === strtoupper($method)) {            if(is_string($params)) {                $real_url = $requestURL. (strpos($requestURL, '?') === false ? '?' : ''). $params;            } else {                $real_url = $requestURL. (strpos($requestURL, '?') === false ? '?' : ''). http_build_query($params);            }            $ch = curl_init($real_url);        } else {            $args = func_get_args();            return false;        }        if ($contentType) {            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));        }        if ($user) {            curl_setopt($ch, CURLOPT_USERPWD, $user);        }        curl_setopt($ch, CURLOPT_HEADER, 0);        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);                $ret = curl_exec($ch);        $info = curl_getinfo($ch);        $contents = array(            'httpInfo' => array(                'send' => $params,                'url' => $requestURL,                'ret' => $ret,                'http' => $info,            )        );        curl_close($ch);        return $ret;    }



System.out.println("Content Type: " + request.getContentType());

此方法之前傳遞的Content Type為text/html

後面把傳值去掉,為空白,傳遞過去的預設值為application/x-www-form-urlencoded

就可以了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.