using curl to automate login

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nancle
Forum Newbie
Posts: 4
Joined: Sun Mar 08, 2009 2:03 pm

using curl to automate login

Post by nancle »

i have a function that uses the "get" method of inputting a keyword string into the URL of a search engine on another website and outputs the results with something like

Code: Select all

 $theurl = 'http://www.examplesite.com/search.php?query=somekeyword';
$contents = file_get_contents($theurl);
print $contents;
 
well now i am applying that process to a search page that requires me to login before i am able to search it (Facebook)

I want my function to do something like this

Code: Select all

[color=#008000]$theurl[/color]='http://www.facebook.com/s.php?q='.[color=#008000]$emailaddress[/color].'&init=q';
[color=#008000]$contents [/color]= file_get_contents([color=#008000]$theurl[/color]);
{
     [color=#40BFFF]//check to see if there are any search results for that email address[/color]
 }
 
HOWEVER, facebook wont let you search with an email address unless you are logged in. So basically I need there to be a function before the one shown above that will automatically log me in to facebook so I can search for the given email address in their search function.

Im assuming the only way to do this is with cURL but im not really familiar with it.

I tried searching on google and found this:

Code: Select all

<?php
// INIT CURL
$ch = curl_init();
 
// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'http://www.external-site.com/Members/Login.php');
 
// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);
 
// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'fieldname1=fieldvalue1&fieldname2=fieldvalue2');
 
// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
 
# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
 
// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);
 
// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL, 'http://www.external-site.com/Members/Downloads/AnnualReport.pdf');
 
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);
 
// CLOSE CURL
curl_close ($ch); 
 
?>
I made an attempt to adapt that to my code, but it stored nothing to "$content"

Any ideas how to get past the facebook login so i can automate member checking?
nancle
Forum Newbie
Posts: 4
Joined: Sun Mar 08, 2009 2:03 pm

Re: using curl to automate login

Post by nancle »

can anyone at least point me in the right direction?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: using curl to automate login

Post by Benjamin »

Yes, you need to use curl. Post your username and password to the login form and view the resulting data sent back from facebook to ensure that the login succeeded.
nancle
Forum Newbie
Posts: 4
Joined: Sun Mar 08, 2009 2:03 pm

Re: using curl to automate login

Post by nancle »

can anyone reccomend some good literature on cURL?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: using curl to automate login

Post by Benjamin »

php.net has some great information on curl.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: using curl to automate login

Post by panic! »

side note: have you thought about using the facebook API?
nancle
Forum Newbie
Posts: 4
Joined: Sun Mar 08, 2009 2:03 pm

Re: using curl to automate login

Post by nancle »

i wasnt aware that facebook had a published api, do you have any references about it?
Post Reply