Site login using cURL(libcurl)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • buzz2050
    New Member
    • Jan 2009
    • 7

    Site login using cURL(libcurl)

    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

    Code:
    <?php
    function getContent($url, $referer, $cookie_file_name, $post_fields='')
    {
    
    	//Setting cookie path	
    		if (substr(PHP_OS, 0, 3) == 'WIN')
    		{
    			if($cookie_file_name != "")
    			{
    				$cookie_file_path = str_replace('\\','/', getcwd().'/'.$cookie_file_name);
    			}
    			else
    			{
    				$cookie_file_path = str_replace('\\','/', getcwd().'/cookies.txt');
    			}
    		}
    		else
    		{
    			if($cookie_file_name != "")
    			{
    				$cookie_file_path = tempnam("/tmp", $cookie_file_name); 			
    			}
    			else
    			{
    				$cookie_file_path = tempnam("/tmp", "cookies"); 			
    			}		
    		}
    	
    	//Creating the cookie file if it doesn't exist
    		$fp = fopen($cookie_file_path, "w");
    		fclose($fp);	
    
    	//Spoofing user-agent
    		$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20";
    
    	//Initializing cURL session and setting appropriate options	
    		$ch = curl_init($url); 
    		
    		curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    		curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    		curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    
    		if($post_fields != '')
    		{
    			curl_setopt($ch, CURLOPT_POST, 1); 
    			curl_setopt($ch, CURLOPT_POSTFIELDS,$post_fields); 
    		}
    
    		if($referer != '')
    		{
    			curl_setopt($ch, CURLOPT_REFERER, $referer);
    		}
    	
    	//Executing cURL session and getting the o/p string
    		$result = curl_exec ($ch);
    
        //Close the cURL session
    		curl_close ($ch);
    	
    	//Return content fetched by the cURL session
    		return $result ;
    
    }//getContent()
    ?>
    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!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    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.

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.


    Learn how to use PHP’s three sets of regular expression functions

    Comment

    • buzz2050
      New Member
      • Jan 2009
      • 7

      #3
      Site login using cURL

      Hey Markus,

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

      Thanks for replying.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by buzz2050
        Hey Markus,

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

        Thanks for replying.
        No problem.

        Greetings,
        Markus.

        Comment

        Working...