473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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

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
3 6259
Markus
6,050 Expert 4TB
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
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
6,050 Expert 4TB
@buzz2050
No problem.

Greetings,
Markus.
Feb 1 '09 #4

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

Similar topics

0
by: Yandos | last post by:
Hello all, I'm sorry for a bit off-topic post, but curl does not have own newsgroup, so I hope someone might help me here... I need to feed form like the following using libcurl: <form...
0
by: mahesh anasuri | last post by:
Hi all, I am new to this mailing list. Thankful if any one is using curl/linux version to and worked on Https. I have created certificates (PEM format) for client and server using openSSL. I...
0
by: Raj | last post by:
Hi, I am newbie to programming with libcurl. the problem is i want to send my gmail username and password outside the browser and get access to my mail. this i am doing with LibCurl. Though i dont...
2
by: Giuseppe | last post by:
Hi everybody, this is my first post in this group since it is from little time that I have begun to learn c++. My problem is to post a web form using the libcurl library. The form is at this...
1
by: Uday | last post by:
Hi, can anybody help me uploading a file (text/zip) to a remote server using libcurl libraries in c++. when i searched in google, i found that i've to use CURLOPT_POST, CURLOPT_INFILE which points...
3
by: JDS | last post by:
So, I'd like to create the following scenario: 1) Use cURL library within PHP (cURL + "Cookie Jar", et.al) to create a virtual browser session that "logs in" to a remote site. (For example: ...
1
by: sharanraman | last post by:
how to send data to a HTTP web server using libcurl
4
by: Choi | last post by:
Good morning. I've tried to extract, using libcurl, web pages but it failed. There is no compilation error concerning the class I wrote, but the problems appear when I compile a main method...
3
by: Jake | last post by:
Hi, I am trying to make an application in C which must establish a telnet session with a remote server, execute some scripts on the remote server and close down the telnet session. I have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.