Solved

Fetch page using curl with cookie

Posted on 2008-06-14
6
774 Views
Last Modified: 2010-04-21
I need to fetch a page with my server that requires a cookie.  If I understand things correctly, I can use curl for this (I have it on my server).
From the example on php.net, I am guessing that I need to use setopt for this?  Can you show me how? I think I use it like:

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "r");

//here is where (I think) I would put the cookie in - it needs to say zipcode=23232

curl_setopt();

curl_exec($ch);
curl_close($ch);
fclose($fp);

//display the page (in real life I will be modifying the text/html with str_replace a bit before displaying it.  I'm not stealing anyone's content by the way, just reformatting it to make my daily research a bit easier).

PRINT $fp;


Thanks,  Chris
0
Comment
Question by:St_Aug_Beach_Bum
6 Comments
 
LVL 3

Expert Comment

by:Xorlev
ID: 21787415
This should work:
curl_setopt(CURLOPT_COOKIE, 'zipcode=23232');

Open in new window

0
 
LVL 3

Expert Comment

by:Xorlev
ID: 21787417
My mistake, forgot to add $ch, before CURLOPT_COOKIE
curl_setopt($ch, CURLOPT_COOKIE, 'zipcode=23232');

Open in new window

0
 
LVL 19

Expert Comment

by:bevhost
ID: 21787434
$cookiejar = "/tmp/cookies.txt";
if (!file_exists($cookiejar)) curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);
else curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);

See full example in code snippet
function web($host,$path,$method,$data="",$password="") {
 

        $cookiejar = "/tmp/cookies.txt";

        $query = ""; $sepCh = "?";

        while (list($k, $v) = each($data)) {

          $query .= $sepCh . urlencode($k) . "=" . urlencode($v);

          $sepCh = "&";

        }
 

        $url = "http://".$host.$path;
 

        $answer = "";
 

        switch ($method) {

              case "GET" :

                //GET METHOD

                if ($fp = fopen($url.$query,"r")) {

                        while (!feof($fp)) {

                                $line = fgets($fp,4096);

                                $answer .= $line;

                        }

                }

                break;
 

              case "POST" :

                // POST METHOD

                $port = 80;

                $data = substr($query,1);
 

                // if php version 4.3 or better $port=443; change host to "ssl://".$host;

                $fp = fsockopen($host, $port, $errno, $errstr, $timeout = 30);
 

                if(!$fp){

                  echo "Error: $errstr ($errno)\n";

                }else{

                  fputs($fp, "POST $path HTTP/1.1\r\n");

                  fputs($fp, "Host: $host\r\n");

                  if ($password) fputs($fp, "Authorization: Basic ".base64_encode($password)."\r\n");

                  fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");

                  fputs($fp, "Content-length: ".strlen($data)."\r\n");

                  fputs($fp, "Connection: close\r\n\r\n");

                  fputs($fp, $data . "\r\n\r\n");
 

                  while (!feof($fp)) {

                        $line = fgets($fp,4096);

                        $answer .= $line;

                  }

                  fclose($fp);

                }

                // END POST METHOD

                break;

              case "CURLPOST" :

                // CURL METHOD

                $url = "https://".$host.$path;

                $data = substr($query,1);

                if (!$ch = curl_init()) {

                    echo "Could not initialize cURL session.\n";

                    exit;

                }

                curl_setopt($ch, CURLOPT_URL, $url);

                //if (!file_exists($cookiejar)) curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);

                //else curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);

                curl_setopt($ch, CURLOPT_POST, 0);

                curl_setopt($ch, CURLOPT_POSTFIELDSIZE, 0);

                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_HEADER, 0);

                curl_setopt($ch, CURLOPT_TIMEOUT, 60);

                //curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');

                //if ($previousUrl) curl_setopt($ch, CURLOPT_REFERER, $previousUrl);

                $output = curl_exec($ch);

                curl_close($ch);

                if($output == ''){

                   echo "cURL did not receive a response back.\n";

                   mail('hosting@bevhost.com','IPN-CURL','cURL did not receive a response back.');

                   exit;

                }

                $answer = preg_replace("'Content-type: text/plain'si","",$output);

                $error_lines = split("\n", $error_message);

                $i=0;

                while($i <= sizeof($error_lines)) {            

                  $error_message_html .= "<p>" .$error_lines[$i];

                  $i++;

                }

                // END CURL METHOD

                break;

             case "CURLGET" :

                // CURL METHOD

                $url = "http://".$host.$path.$query;

                if (!$ch = curl_init()) {

                    echo "Could not initialize cURL session.\n";

                    exit;

                }

                curl_setopt($ch, CURLOPT_URL, $url);

                if (!file_exists($cookiejar)) curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiejar);

                else curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar);

                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                curl_setopt($ch, CURLOPT_HEADER, 1);

                curl_setopt($ch, CURLOPT_TIMEOUT, 60);

                curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');

                if ($previousUrl) curl_setopt($ch, CURLOPT_REFERER, $previousUrl);

                $output = curl_exec($ch);

                curl_close($ch);

                if($output == ''){

                   echo "cURL did not receive a response back.\n";

                   exit;

                }

                $answer = preg_replace("'Content-type: text/plain'si","",$output);

                $error_lines = split("\n", $error_message);

                $i=0;

                while($i <= sizeof($error_lines)) {

                  $error_message_html .= "<p>" .$error_lines[$i];

                  $i++;

                }

                // END CURL METHOD

                break;

        } //switch method
 

        $previousUrl = $url;
 

        return $answer;

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21787437
I'm not sure what's that fopen for. If you are trying to retrieve http://www.example.com/example_hompage.txt, then you do not need fopen. To retrieve the page you need either curl OR fopen, but not both. However, since you need to pass cookies, you cannot use fopen. Try:

<?
$string = 'cookie=atestcookie'; 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/example_homepage.txt");
curl_setopt($ch, CURLOPT_COOKIE, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$text = curl_exec($ch);
curl_close($ch);
echo($text);
?> 

Open in new window

0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 31467293
Thank you all.  Overwhelmed a bit by several answers, but the most through understandable, straightening me out answer was from Helio. Appreciate the help from everyone though!   Chris
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 21787486
opps, sorry, that should be Hielo  !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 81
Having trouble setting Apache to recognise 2 vitual sites (on different drive). 3 54
mysqli 3 18
Checking if varaible is empty 6 25
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now