PHP中PostMethod of Java的等价物

时间:2015-03-17 03:00:31

标签: java php codeigniter http-post

我想问一下是否存在一个PHP函数来模拟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似乎不起作用,而$ this-> output-> set_output正确传递数据但无法捕获requestUrl的响应。

谢谢。

1 个答案:

答案 0 :(得分:0)

我能够使用HTTP POST from PHP, without cURL上找到的代码块来捕获requestUrl的响应(非常感谢)。

$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);