简体   繁体   中英

Equivalent of PostMethod of Java in PHP

I would like to ask if there exists a PHP function to simulate this block of code in Codeigniter.

HttpClient httpClient       = new HttpClient();
PostMethod postMethod       = new PostMethod(requestURL);
NameValuePair[] datas       = {new NameValuePair("studentnumber", studentnumber), 
                              new NameValuePair("studentdata", encryptedData)};

postMethod.setRequestBody(datas);
int statusCode      = httpClient.executeMethod(postMethod);
byte[] responseByte = postMethod.getResponseBody();
String responseBody = new String(responseByte, "UTF-8");

curl doesn't seem to work, while $this->output->set_output passes the data properly but fails to catch the response of the requestUrl.

Thank you.

I was able to catch the response from requestUrl using this block of code that I found on HTTP POST from PHP, without cURL (thanks a lot for this).

$options = array(
    'http' => array(
        'method' => "POST",
        'header' => "Accept-language: en\r\n" . "Content-type: application/x-www-form-urlencoded\r\n",
        'content' => http_build_query(array(
            'studentnumber' => $studentnumber,
            'studentdata' => $encryptedData,
        ),'','&'
    )
));

$refno = file_get_contents($requestUrl,false,$context);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM