Jump to content

cURL "Login" with cookies?


Dale_G

Recommended Posts

Hey everyone, so like I'm new to cURL and I was wondering if it was possible to successfully "login" to a site such as say...MySpace using cURL. Or retrieve data only available whe logged in from a site that has a session ID in the URL to keep you logged on, such as www.mysite.com/index.php?id=le4377sdswe-==.

 

So basically, I'm hoping cURL will shine where file_get_contents fails!

 

Just uh, some external references I've found if they may help anybody!

 

http://curl.haxx.se/mail/curlphp-2001-10/0015.html

 

http://www.weberdev.com/get_example-4555.html

Link to comment
Share on other sites

This is absolutely possible.

 

i believe these curl options will help you

 

example

curl_setopt( $ch, CURLOPT_COOKIE, "cookie=value&othercookie=value" );//Cookies
curl_setopt( $ch, CURLOPT_POST, 1 );//Post
curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=blah&password=blah" );//Post

 

You may want to look into doing this with sockets (socket_ lib or fsockopen)

 

Cookies:

$sock = fsockopen( "www.myspace.com", 80, $errno, $errstr, 5 );
fwrite( $sock, "GET / HTTP/1.1\r\n" );
fwrite( $sock, "Host: www.myspace.com\r\n" );
fwrite( $sock, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0\r\n" );
fwrite( $sock, "Set-Cookie: cookie\r\n" );
fwrite( $sock, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" );
fwrite( $sock, "Keep-Alive: 300\r\n" );
fwrite( $sock, "Connection: keep-alive\r\n" );
while( !feof( $sock ) ) { print fread( $sock, 1024 ); }
fclose( $sock );

 

Post:

fwrite( $sock, "POST / HTTP/1.1\r\n" );
fwrite( $sock, "Host: www.myspace.com\r\n" );
fwrite( $sock, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0\r\n" );
fwrite( $sock, "Content-Length: 28\r\n" );
fwrite( $sock, "Content-Type: application/x-www-form-urlencoded\r\n" );
fwrite( $sock, "\r\nusername=blah&password=blah\r\n" );
fwrite( $sock, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" );
fwrite( $sock, "Keep-Alive: 300\r\n" );
fwrite( $sock, "Connection: keep-alive\r\n" );
while( !feof( $sock ) ) { print fread( $sock, 1024 ); }
fclose( $sock );

 

It's up to you.  Sockets are faster but http requests get a little tricky. Sorry for the long post, if you need more help, I will be happy to assist.

Link to comment
Share on other sites

Alright..thanks, pretty sure I'm doing something wrong but, I have this code so far exactly, with the exception of the user and passes.

 

<?php

$sock = fsockopen( "www.myspace.com", 80, $errno, $errstr, 5 );
fwrite( $sock, "GET / HTTP/1.1\r\n" );
fwrite( $sock, "Host: www.myspace.com\r\n" );
fwrite( $sock, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0\r\n" );
fwrite( $sock, "Set-Cookie: cookie\r\n" );
fwrite( $sock, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" );
fwrite( $sock, "Keep-Alive: 300\r\n" );
fwrite( $sock, "Connection: keep-alive\r\n" );

fwrite( $sock, "POST / HTTP/1.1\r\n" );
fwrite( $sock, "Host: www.myspace.com\r\n" );
fwrite( $sock, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008052906 Firefox/3.0\r\n" );
fwrite( $sock, "Content-Length: 28\r\n" );
fwrite( $sock, "Content-Type: application/x-www-form-urlencoded\r\n" );
fwrite( $sock, "\r\nusername=blah&password=blah\r\n" );
fwrite( $sock, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" );
fwrite( $sock, "Keep-Alive: 300\r\n" );
fwrite( $sock, "Connection: keep-alive\r\n" );
while( !feof( $sock ) ) { print fread( $sock, 1024 ); }
fclose( $sock );

?>

 

And I'm getting this totally epic error!

HTTP/1.0 411 Length Required Server: squid/2.6.STABLE12 Date: Wed, 09 Jul 2008 21:04:33 GMT Content-Type: text/html Content-Length: 1628 Expires: Wed, 09 Jul 2008 21:04:33 GMT X-Squid-Error: ERR_INVALID_REQ 0 X-Cache: MISS from wc01.inet.mesa1.secureserver.net Connection: close

ERROR

The requested URL could not be retrieved

 

While trying to process the request:

 

GET / HTTP/1.1

Host: www.myspace.com

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9) Gecko/2008052906 Firefox/3.0

Set-Cookie: cookie

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Keep-Alive: 300

Connection: keep-alive

Host: www.myspace.com

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9) Gecko/2008052906 Firefox/3.0

Content-Length: 28

Content-Type: application/x-www-form-urlencoded

 

 

The following error was encountered:

 

    * Invalid Request

 

Some aspect of the HTTP Request is invalid. Possible problems:

 

    * Missing or unknown request method

    * Missing URL

    * Missing HTTP Identifier (HTTP/1.0)

    * Request is too large

    * Content-Length missing for POST or PUT requests

    * Illegal character in hostname; underscores are not allowed

 

Your cache administrator is webmaster.

Generated Wed, 09 Jul 2008 21:04:33 GMT by wc01.inet.mesa1.secureserver.net (squid/2.6.STABLE12)

 

Any ideas?

Link to comment
Share on other sites

You cannot use both codes at the same time, that result is not a php error but an error from myspace's server because you made an invalid request.

 

Also, the line GET / HTTP/1.1 needs to be changed to GET /path/to/page/index.html or whatever, same with post.  (For post you need t know where the index page posts to, I can't remember it off the top of my head).  If you are completely new to HTTP requests, it will be better to stick to cURL, using the CURLOPT_COOKIE option.

Link to comment
Share on other sites

You cannot use both codes at the same time, that result is not a php error but an error from myspace's server because you made an invalid request.

 

Also, the line GET / HTTP/1.1 needs to be changed to GET /path/to/page/index.html or whatever, same with post.  (For post you need t know where the index page posts to, I can't remember it off the top of my head).  If you are completely new to HTTP requests, it will be better to stick to cURL, using the CURLOPT_COOKIE option.

 

Alrighty, uh, mind sharing what to do again exactly? Like, exactly what I should put from <?php TO ?>, because..I've put what you put, the first part, the second part, then both, then separate, only to get a blank page, a non loading page, or the page itself with none of the cookies applied.

 

So yeah that would be super if you can help me out here!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.