1

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;
}
D_R
  • 4,894
  • 4
  • 45
  • 62

1 Answers1

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);
The Humble Rat
  • 4,586
  • 6
  • 39
  • 73
  • for somereason im getting "301 Moved Permanently" error if im not using `curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);`.. maybe thats what causing the problem? – D_R Oct 23 '13 at 08:40
  • A 301 or 302 are redirects. You can instruct CURL to follow them to the destination URL. http://stackoverflow.com/questions/3519939/make-curl-follow-redirects – aross Oct 23 '13 at 09:14