tags:

views:

186

answers:

2
+1  Q: 

CURL usages in PHP

Hi friends,

I had a doubt in CURL (PHP) . I want to login directly into a site using CURL. Is it possible, if possible please guide me.

I tried with the following but its not working http://username:password@myurl

Please solve this..

Thanks in advance

+3  A: 

This is probably what you're looking for:

$ch = curl_init("http://myurl");
curl_setopt($ch, CURLOPT_USERPWD, "[username]:[password]");
$output = curl_exec($ch);

You can find more options for CURL here.

Kyle Cronin
CURLOPT_HTTPAUTH is not needed it seems, but can be used to specify the auth method if required/wanted.
OIS
+1  A: 

praveenjayapal, are you trying to log in to a site that uses HTTP-authentication? Or does the site have some sort of username/password form that you fill?

In the second case, CURL is probably not the right solution, since such sites usually store some sort of authentication cookie in your browser after you've successfully logged-in.

In order to simulate the browser's behaviour, you'd have to send the POST/GET request that submits your username and password, and then read the cookies returned in the HTTP headers. (There might be a way to get CURL to spit them out.) Once you had the cookie, you could then continue to access the site normally, as long as you kept sending the cookie.

Please note: Many sites do not use behaviour as simple as what I decribed above. But most PHP-based sites simply use PHP-sessions to track users as they browse the site. Which means that you need to "tell" the site that you belong to a session. Depeneding on how the PHP on the website was configured, this can be done by storing and sending the phpsessid cookie, or phpsessid as a GET parameter in every request.

scraimer