2014-12-02 93 views
0

I used curl before, I want to send data by POST from location A to location B, and the location in the end is B not A. while in fact the last URL is A using curl, not what I actually want , how to solve this problem?how to send post to remote url and redirect to the url in php

我之前用過curl這個方法,我希望通過POST方法把數據從地址A發送到地址B,而且最後的地址是B而不是A。但是事實上用curl的話最後的地址是A,不是我事實上期望的,怎樣解決這個問題?

my code(I used CI framework):

<?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']; 
    } 

} 

I want it to work like that in Java as this:

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

I'm not sure I follow completely, but if you want to POST data from A to B, and then redirect the user in their web browser to B, just send a header to the user after the POST completes, e.g. 'header("Location: http://b.example.com)'. – 2014-12-02 16:01:58

+0

I think what you're looking for is a basic form functionality, set the action attribute of the form tag to location B - no need to use curl. – AnchovyLegend 2014-12-02 16:02:35

+0

if not use curl, how to do? – Muguan 2014-12-02 16:14:09

回答

0

You could try using stream_context_create and file_get_contents for this, and send your data as an array.

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

The other end will receive it as:

$act = $this->input->post('act'); 
    $botton = $this->input->post('botton'); 
+0

thanks for your answer,however, it doesn't works. I entered the url "http://localhost/Test2/index.php/see/a" and there is nothing display,the URL is still a – Muguan 2014-12-02 16:43:44

+0

Leandro Dimitrio: I'm sorry, it still not works. – Muguan 2014-12-03 16:49:47

+0

Are they different servers? What error messages do you get? – 2014-12-04 17:58:10