473,569 Members | 2,691 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Site login using cURL(libcurl)

7 New Member
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 6285
Markus
6,050 Recognized Expert Expert
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
buzz2050
7 New Member
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 Recognized Expert Expert
@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
4471
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 action="file-upload.php" method="post" enctype="multipart/form-data"> <input name="senderid" value="1111" type="hidden"> <input name="senderpass"...
0
10700
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 am using Apache server/Linux platform and started server with "server certificate". I configured server to listen HTTPS service at port 443. I...
0
1470
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 get any errors, i am nt able to get the o/p Can anyone kindly help. i will attach the program here #include <curl/curl.h> #include <curl/types.h>...
2
6697
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 address: http://it.mobi.dada.net/lostpassword.php end it isn't very complicated: <form method="post" action="/cgi-bin/hotw/sendpassword.chm">
1
3327
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 to the actual file & READFUNCTION which reads the content from the file and puts in the buffer. i'm setting the file size also in...
3
10353
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: here: http://curl.haxx.se/libcurl/php/examples/cookiejar.html and here: http://curl.haxx.se/libcurl/php/examples/ebay_login.html
1
1458
by: sharanraman | last post by:
how to send data to a HTTP web server using libcurl
4
3001
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 which calls this class. My class : **********************************libcurl_tools.h
3
5333
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 had a look at libcurl since it supports several protocols, but I haven't been
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.