Question

$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$useragent="Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16";

$ch = curl_init ("website.com");
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec ($ch);

The website sets a cookie and then redirects. Would this code suffice? Because it seems to be not getting the cookie properly. How can I check to see if it's set? Better yet, if I know what cookies I want can I just make it or something?

Edit: So my CURL script visits the website right? The website sets cookies for validation, and I want to see if my cURL script is receiving those cookies properly. I want to know if there's a test for that, and/or I want to know if I can just create a cookie to validate for the website.

Was it helpful?

Solution

I recently had a project where I needed to pass cookies between servers, and I found that setting both CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR to the same file did the trick.

If you're just wanting to see if the cookies from the remote site are being set, you should be able to look at the CURLOPT_COOKIEJAR file in a text editor.

OTHER TIPS

If this were the command line curl you'd throw in the -v parameter. Pay attention to the lines starting with > Cookie:.

The php version of this is:

curl_setopt($ch,CURLOPT_VERBOSE,TRUE);//and set CURLOPT_STDERR to STDOUT

And if this were a browser I would debug with one of these: you can use Fiddler on IE, TamperData or Firebug (net) on Firefox, the inspector on Chrome and on Safari, or a proxy with any browser to watch how the form is submitted by a "normal" browser.

The proxy approach might work if you set php's curl to use it.

curl_setopt($ch,CURLOPT_HTTPPROXYTUNNEL,TRUE);//& CURLOPT_PROXYPORT CURLOPT_PROXY

If you want to just add cookies to one request you could write a line like:

$ch->headers[] = 'Cookie: recent=543..; _session_id=6185..; __utma=572.1.1.1.1; __utmc=572..; __utmz=572.1.1.1.1.utmccn=(referral)|utmcsr=domain.com|utmcct=/request/path|utmcmd=referral';

but I'm not certain about that one, these might be more along the same line:

curl_setopt($ch,CURLOPT_COOKIE,"recent=543..; _session_id=618..");
// Possibly, but I think this might overwrite other headers.
curl_setopt($ch,CURLOPT_HTTPHEADER, "Cookie: recent=543..; _session_id=6185..");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top