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
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!
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 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!
Comment