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

1 Answer 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);
Sign up to request clarification or add additional context in comments.

2 Comments

for somereason im getting "301 Moved Permanently" error if im not using curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);.. maybe thats what causing the problem?
A 301 or 302 are redirects. You can instruct CURL to follow them to the destination URL. stackoverflow.com/questions/3519939/make-curl-follow-redirects

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.