By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,948 Members | 1,929 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,948 IT Pros & Developers. It's quick & easy.

Site login using cURL(libcurl)

P: 7
Hi all,

I am using cURL for the first time. I need to login to a site and my cURL code to do the same is as follows:

//curlScript.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. function getContent($url, $referer, $cookie_file_name, $post_fields='')
  3. {
  4.  
  5.     //Setting cookie path    
  6.         if (substr(PHP_OS, 0, 3) == 'WIN')
  7.         {
  8.             if($cookie_file_name != "")
  9.             {
  10.                 $cookie_file_path = str_replace('\\','/', getcwd().'/'.$cookie_file_name);
  11.             }
  12.             else
  13.             {
  14.                 $cookie_file_path = str_replace('\\','/', getcwd().'/cookies.txt');
  15.             }
  16.         }
  17.         else
  18.         {
  19.             if($cookie_file_name != "")
  20.             {
  21.                 $cookie_file_path = tempnam("/tmp", $cookie_file_name);             
  22.             }
  23.             else
  24.             {
  25.                 $cookie_file_path = tempnam("/tmp", "cookies");             
  26.             }        
  27.         }
  28.  
  29.     //Creating the cookie file if it doesn't exist
  30.         $fp = fopen($cookie_file_path, "w");
  31.         fclose($fp);    
  32.  
  33.     //Spoofing user-agent
  34.         $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20";
  35.  
  36.     //Initializing cURL session and setting appropriate options    
  37.         $ch = curl_init($url); 
  38.  
  39.         curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  40.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  41.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  42.         curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
  43.         curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
  44.  
  45.         if($post_fields != '')
  46.         {
  47.             curl_setopt($ch, CURLOPT_POST, 1); 
  48.             curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields); 
  49.         }
  50.  
  51.         if($referer != '')
  52.         {
  53.             curl_setopt($ch, CURLOPT_REFERER, $referer);
  54.         }
  55.  
  56.     //Executing cURL session and getting the o/p string
  57.         $result = curl_exec ($ch);
  58.  
  59.     //Close the cURL session
  60.         curl_close ($ch);
  61.  
  62.     //Return content fetched by the cURL session
  63.         return $result ;
  64.  
  65. }//getContent()
  66. ?>
  67.  
I have xampp installed on my localhost and right now I am executing the script there.

I did manage to login to my account, however, when I click on any of the links in the fetched content, it tries to redirect to some resource on my localhost and consequently gives the 404-object not found error.

Looking at the grabbed content I realized that all the links there have relative paths, hence 'http://localhost' gets prepended automatically.

Is replacing all relative paths with absolute paths the only way to make the links work? But in any case, my browser address bar still shows http://localhost/curlScript.php, so do I need to redirect to the appropriate
page after logging in.. I mean how would that work.. am I missing something?

I am a little confused, any help highly appreciated!!!

Thanks!
Jan 30 '09 #1
Share this Question
Share on Google+
3 Replies


Markus
Expert 5K+
P: 6,050
Of course you'll have to change the links on your fetched content. Otherwise, the links will attempt to point to a location on your local server. You can use a regular expression to filter through the content and preg_replace() to change the links.

http://uk3.php.net/preg_replace

http://www.regular-expressions.info/php.html
Jan 30 '09 #2

P: 7
Hey Markus,

I thought so..
Well, will try using regular expressions to match and replace links and see what happens..

Thanks for replying.
Feb 1 '09 #3

Markus
Expert 5K+
P: 6,050
@buzz2050
No problem.

Greetings,
Markus.
Feb 1 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.