Jump to content

php login to other site using curl


fix3r

Recommended Posts

<?php

$headers = array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0. Gecko/20061025 Firefox/1.5.0.8");

$url="THE WEBSITE";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "email=email&pass=pass");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$store = curl_exec ($ch);
curl_close ($ch);
print_r($store);

?>

 

Okay, I got the login to work. Now, how would I make it go to an existing page with the login information and then submit a whole new form? That I am a little confused about.

Okay, sorry for spam. It seems that this code still outputs the html of the site. How do I make it not output the site at all?

 

$url="WEBSITE";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookies1.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies1.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "ALL MY FIELDS");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$store = curl_exec ($ch);
curl_close ($ch);
//print($store);

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.externalsite.co.uk/login.php");

curl_setopt ($ch, CURLOPT_POST, 1);

curl_setopt ($ch, CURLOPT_POSTFIELDS, "username=foo&password=bar");

curl_setopt ($ch, CURLOPT_COOKIEJAR, "cookie.txt");

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

$login = curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL, "http://www.externalsite.co.uk/memberspage.php");

$members = curl_exec ($ch);

curl_close ($ch);

?>

You now have a variable, $members, which contains the HTML output of memberspage.php after logging in with the username foo and password bar.

Hope that helps.. and works! 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.