Current location: Hot Scripts Forums » Programming Languages » PHP » curl + post + cookies


curl + post + cookies

Reply
  #1 (permalink)  
Old 08-28-09, 09:12 AM
naissa naissa is offline
Wannabe Coder
 
Join Date: Jul 2004
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
curl + post + cookies

PHP Code:

    $cookie_jar tempnam('/tmp','cookie');

        
// INIT CURL
        
$ch curl_init();
        
curl_setopt($chCURLOPT_TIMEOUTself::CURL_TIMEOUT);
        
// SET URL FOR THE POST FORM LOGIN
        
curl_setopt($chCURLOPT_URLself::LOGIN);
        
// ENABLE HTTP POST
        
curl_setopt ($chCURLOPT_POST1);
        
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
        
curl_setopt ($chCURLOPT_POSTFIELDS'email=' self::USR '&password=' self::PASS);
        
curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue);
        
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
        
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
        # not to print out the results of its query.
        # Instead, it will return the results as a string return value
        # from curl_exec() instead of the usual true/false.
        
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
        
curl_setopt ($chCURLOPT_COOKIEJAR$cookie_jar);
        
curl_setopt ($chCURLOPT_COOKIEFILE$cookie_jar);
        
curl_setopt($chCURLOPT_USERAGENT"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
        
// EXECUTE 1st REQUEST (FORM LOGIN)
        
$store curl_exec ($ch);
        
        if(!
$store) return false;
        
        
// SET FILE TO DOWNLOAD
        
curl_setopt($chCURLOPT_URLself::SITE $this->domain);
        
        
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
        
$content curl_exec ($ch);
        
$this->content $content;
        
// CLOSE CURL
        
curl_close ($ch);
        
        unset(
$ch);
        
unlink($cookie_jar);
        
        return 
$this->content
So I'm trying to access a site using curl. First, I login to the site using curl's post variables. Then I access a page on the site to retrieve data (after logging in using curl). I'm I doing the right thing above? Have I used the right code? Is it necessary to have these two lines of code:

curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie_jar);

??

Thanks
Reply With Quote
  #2 (permalink)  
Old 08-30-09, 06:36 AM
dgreenhouse's Avatar
dgreenhouse dgreenhouse is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: San Francisco
Posts: 457
Thanks: 0
Thanked 3 Times in 3 Posts
I haven't looked at it closely, but it looks about right.

Pretty much if you're trying to mimic a browser and the page your code's visiting is expecting
cookies to be enabled - you need the cookie lines and associated file(s). If you'll be logging in each
time and cookies aren't required, you can probably eliminate those lines and the need for the
"cookiejar" file and path to save cookies when the curl session ends.

Is it working?

Actually... Now that I look at it...

It doesn't appear that you need the cookie stuff and if it's working, it doesn't appear that the target server requires cookies be set.
As a matter of fact, you're just creating a temporary file with nothing in it and deleting it when the session's over.
That as you surmised would be a waste and make it hard to comprehend the code in 6 months.

Exploring the cookie file a little more...

The cookie file would be unnecessary unless of course that somewhere earlier in the code, the temp cookie file
gets loaded with actual valid cookie data that the remote server wants and needs to munch on...

Last edited by dgreenhouse; 08-30-09 at 06:49 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php, Curl and some cookies (not the tasty ones) -tg PHP 0 02-20-09 05:12 PM
Using CURL to send POST Data tophat PHP 2 05-04-07 02:53 AM
curl and browser enabled cookies klaniak PHP 1 10-20-06 12:54 AM
Can't seem to get the cookies to the cookie_jar benny666 Perl 1 10-28-03 04:54 AM


All times are GMT -5. The time now is 07:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.