Remote Login with Curl PHP
The Curl Library of PHP can be used to open remote web pages and also logging in where a login is required.
Code
$login_url = 'http://www.somesite.com/login.php'; #These are the post data username and password $post_data = 'username=someusername&password=somepassword'; #Create a curl object $ch = curl_init(); #Set the useragent $agent = $_SERVER["HTTP_USER_AGENT"]; curl_setopt($ch, CURLOPT_USERAGENT, $agent); #Set the URL curl_setopt($ch, CURLOPT_URL, $login_url ); #This is a POST query curl_setopt($ch, CURLOPT_POST, 1 ); #Set the post data curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); #We want the content after the query curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); #Follow Location redirects curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); /* Set the cookie storing files Cookie files are necessary since we are logging and session data needs to be saved */ curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); #Execute the action to login $postResult = curl_exec($ch);
After the curl_exec the login page is opened with login details or simply the login has been done. Now any other url (which is login restricted) can be opened by
curl_setopt($ch, CURLOPT_URL, $url); curl_exec($ch);
The option CURLOPT_COOKIEFILE is used to specify a file which stores the cookie data that is received during the process of opening and logging into webpages. Make sure the directory is writeable by php so that it can create these files.
The login page of the particular site where the username and password are asked for will give various information regarding the name of fields to be send as username , password , the type of query that is POST , the form submission url to which the values are to be submitted etc. Those information should be used in the above code to specify the login url , username and password field names and values etc.
The same should work with ssl or https urls provided the openssl module is enabled.
Popularity: 6% [?]















