2013-10-23 54 views
1

我在$ url上打印了$_REQUEST$_POST變量..只顯示一個空數組.. 我做錯了什麼?CURL不通過帖子參數

function redirect_post($url, $data, $headers = null) 
{ 
    // Url Encoding Data 
    $encdata = array(); 

    foreach ($data as $key => $value) { 
     $encdata[$key] = urlencode($value); 
    } 

    //url-ify the data for the POST 
    $encdata_string = http_build_query($encdata); 

    //open connection 
    $ch = curl_init(); 

    //set the url, number of POST vars, POST data 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true); 
    curl_setopt($ch,CURLOPT_POST,count($encdata)); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$encdata_string); 

    //execute post 
    $result = curl_exec($ch); 

    echo $result; 
    die; 
} 

回答

1

請嘗試使用以下代碼。它看起來像你需要把你的URL放在curl_init中,然後使用curl_setopt發送$ data來發送數組。

$userIp = array('userIp'=>$_SERVER['REMOTE_ADDR']); 
$url = 'http://xxx.xxx.xxx.xxx/somedirectory/somescript.php'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $userIp); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 
curl_close($ch); 
+0

爲somereason IM即時通訊,如果不使用'curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,真)得到 「301永久移動」 的錯誤;'..也許那是什麼導致了問題? – DanR

+0

301或302是重定向。您可以指示CURL將其關注到目標網址。 http://stackoverflow.com/questions/3519939/make-curl-follow-redirects – aross