tags:

views:

65

answers:

1

I have been experimenting with curl for accessing the PayPal payment authorisation site using PHP.

e.g.

...
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch, CURLOPT_HEADER, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $nvp);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $res = curl_exec ($ch);

   preg_match_all('/Set-Cookie: .*/', $res, $cookieMatches);

   foreach ($cookieMatches[0] as $cookieMatch)
      header($cookieMatch);

   preg_match('/Location: .*/', $res, $locMatches);
   header($locMatches[0]);

   header('Vary: Accept-Encoding');
   header('Strict-Transport-Security: max-age=500');
   header('Transfer-Encoding: chunked');
   header('Content-Type: text/html');

The principle being simply to reflect the original redirect (I am sure there is a simpler way to do this). However, the response from PayPal seems to indicate some kind of cookie error.

My hunch is that the cookie has been linked to the originating machine in some way. Can anyone confirm this, or am I just missing something obvious!

A: 

The CURL has built-in support for cookies (as you know). But it's been tricky. I haven't managed cookies to work until I declared option

curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

Third parameter is a name of the file storing cookies - preferably in temp folder. Maybe you should just try this approach.

With this the redirects work "automatically".

Tomasz Struczyński
Thank you for the suggestion - and also CURLOPT_COOKIEFILE should be set. I tried this approach initially but the PayPal billing details or log in submission forms then fail. In comparison with a direct HTML form submission, a number of cookies seem to be missing from the browser. An HTTP sniffer confirmed this - which is why I tried the above.
mayfly