1

我在 $url$_REQUEST$_POST变量上打印.. 只显示一个空数组.. 我做错了什么?

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;
}
4

1 回答 1

1

Try using the following code. It looks like you need to put your URL in the curl_init and then send the $data over using the curl_setopt to send the array.

$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);
于 2013-10-23T08:38:12.300 回答