[plug] Anyone know PHP + cURL?
Lucas van Staden
lvs at dedmeet.com
Wed Dec 1 16:49:28 WST 2010
Hi,
The first think you need to do is automate the login, and store the
cookie used to keep you authed :
To get the postcars, I use a ff plugin called 'Tamper Data'.
I open it and login on the page I want, login, and grab the session vars.
Note any @ sign must be put back (they will be url encoded)
These are from a class I wrote a while back to login to a supplier site
and pull their products.
This is the main login post, and is flagged as done by the class var
_isLoggedIn (bool)
$this->_chkfile is set in the constructor, to any file, mine is
/tmp/cookiejar
public function Login() {
$ckfile = $this->_chkfile;
$postvars = "YOUR POST VARS HERE"
$posturl = "THE URL THE LOGIN FORM POSTS TO"
$ch = curl_init($posturl);
curl_setopt ($ch, CURLOPT_REFERER, $posturl);
curl_setopt ($ch, CURLOPT_URL, $posturl);
$url = curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);
curl_setopt($ch, CURLOPT_HEADER, True); // DO NOT RETURN HTTP
HEADERS
curl_setopt($ch, CURLOPT_HEADEROUT, False); // DO NOT RETURN
HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True); // RETURN THE
CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIESESSION, True);
curl_setopt($ch, CURLOPT_VERBOSE, false); // switch on to see
what curl is up to.
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible;
MSIE 5.01; Windows NT 5.0)');
$data = curl_exec($ch);
if ($data == false){
die('Could not save/logon');
}
curl_close($ch);
// add code to test if this was in fact true.
$this->_isLoggedIn = True;
return $data;
}
Then I have the send with cookie routine, that calls any further urls
with the cookie, and also logs in if I was not
public function sendWithCookie($url) {
if ($this->_isLoggedIn == False) {
$this->Login();
}
$ckfile = $this->_chkfile;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible;
MSIE 5.01; Windows NT 5.0)');
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
if (empty($data)){
$this->_isLoggedIn == False;
die('Send with cookie failed');
}
curl_close($ch);
return $data;
}
On some site, I have found that you must first load teh homepage, so
their internal 'can you use cookies' stuff is setup.
public function loadhomepageCookies($url){
$ckfile = $this->_chkfile;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, True);
curl_setopt($ch, CURLOPT_HEADER, True); // DO NOT RETURN HTTP
HEADERS
curl_setopt($ch, CURLOPT_HEADEROUT, False); // DO NOT RETURN
HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True); // RETURN THE
CONTENTS OF THE CALL
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIESESSION, True);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch , CURLOPT_SSL_VERIFYHOST , false );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible;
MSIE 5.01; Windows NT 5.0)');
$data = curl_exec($ch);
curl_close($ch);
}
Hope this helps you get on track
-Lucas
On 01/12/10 13:45, Shanon Loughton wrote:
> Hi all
>
> Im trying to extract our monthly telstra bill from the hideous portal
> https://www.telstrabusiness.com/business/login.jsp
> And I know the manual sequence with POST data and URLs except I cant
> seem automate it with Ubuntu, Apache, PHP & cURL, specifically logging in.
>
> If anyone can help that would be appreciated?
>
> thanks
> Shanon
>
>
> _______________________________________________
> PLUG discussion list: plug at plug.org.au
> http://www.plug.org.au/mailman/listinfo/plug
> Committee e-mail: committee at plug.linux.org.au
>
--
Regards
Lucas van Staden
http://www.proxiblue.com.au/ (Consumer Electronics Retail Shop)
http://www.vanstaden.com.au/ (Linux)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.plug.org.au/pipermail/plug/attachments/20101201/63a5fefa/attachment.html>
More information about the plug
mailing list