之前我使用过curl,我想通过POST从位置A向位置B发送数据,最后的位置是B而不是A.实际上最后一个URL是A使用curl,而不是我真正想要的,如何解决这个问题?
我之前用过卷曲这个方法,我希望通过POST方法把数据从地址甲发送到地址B,而且最后的地址是乙而不是A.但是事实上用卷曲的话最后的地址是A,不是我事实上期望的,怎样解决这个问题?
我的代码(我使用了CI框架):
<?php
class See extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function a(){
$url='http://localhost/Test2/index.php/see/b';
$data=array(
'act'=>'botton',
'foo'=>'bar'
);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
}
public function b(){
echo $_POST['act'];
echo '<br/>';
echo $_POST['foo'];
}
}
我希望它像Java一样工作:
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000); // 设置连接超时为5秒
HttpPost request = new HttpPost(httpUrl);
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
request.setEntity(entity);
if (JSESSIONID != null) {
request.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
// System.out.println(JSESSIONID+"哈哈");
}
DefaultHttpClient client = new DefaultHttpClient(httpParams);
// 取得HTTP response
HttpResponse response = client.execute(request);
// 如果返回状态为200,获得返回的结果
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
byte[] bytes = EntityUtils.toByteArray(response.getEntity());
CookieStore mCookieStore = client.getCookieStore();
List<Cookie> cookies = mCookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
// 这里是读取Cookie['SESSIONID']的值存在静态变量中,保证每次都是同一个值
if ("JSESSIONID".equals(cookies.get(i).getName())) {
JSESSIONID = cookies.get(i).getValue();
break;
}
}
// System.out.println(new String(bytes,"UTF-8"));
return bytes;
} else
System.out
.println("状态码" + response.getStatusLine().getStatusCode());
return "连接错误!".getBytes();
答案 0 :(得分:0)
您可以尝试使用stream_context_create
和file_get_contents
,并将数据作为数组发送。
$url = "http://localhost/Test2/index.php/see/b";
$data = array('act' => 'botton', 'botton' => 'bar');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
另一端将收到:
$act = $this->input->post('act');
$botton = $this->input->post('botton');