D_R
D_R

Reputation: 4972

CURL not passing post parameters

Im printing on the $url the $_REQUEST and $_POST variables.. only showing an empty array.. what am I doing wrong?

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

Upvotes: 1

Views: 822

Answers (1)

The Humble Rat
The Humble Rat

Reputation: 4696

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

Upvotes: 1

Related Questions