0

Any idea why this function is not redirecting to paypal.com and instead I am staying on the same page? curl is activated on the server, safe mode is off and openbasedir is turned off too.

function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
curl_post("https://www.paypal.com/cgi-bin/webscr",$_POST);

Any idea what am I doing wrong?

Andy G
  • 19,232
  • 5
  • 47
  • 69
  • 1
    AS far as I know, curl just makes http requests. It doesn't redirect, or act as any kind of web browser. – CP510 Sep 04 '13 at 17:49

2 Answers2

3

After you make curl request:

foreach ($parameters as $key => $value) {
    $requestParams[] = $key . '=' . $value;
}
$requestParams = implode ('&', $requestParams);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER);
curl_setopt( $ch, CURLOPT_AUTOREFERER, true);
$result = curl_exec($ch);

You should get exact url, to know where you should be redirected:

$location = curl_getinfo($ch)['redirect_url'];

And then redirect:

header('Location: '. $location);
0

cURL is just a library for initiating and firing HTTP and FTP requests. Your code just sends a HTTP POST request to the PayPal server.

In order to redirect the user, send a Location header (before any output!):

header('Location: https://www.paypal.com/...');
ComFreek
  • 29,044
  • 18
  • 104
  • 156