tags:

views:

229

answers:

4

I'm new to using cURL and its hard to find good resources for it. What I'm trying to do is login to a remote site, by having curl do the login form and then send back that it was successful.

The code I have doesn't seem to work and only tries to show the main page of the site.

    $username="[email protected]"; 
$password="mypassword"; 
$url="http://www.myremotesite.com/index.php?page=login"; 
$cookie="cookie.txt"; 

$postdata = "email=".$username."&password=".$password; 

$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt ($ch, CURLOPT_REFERER, $url); 

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata); 
curl_setopt ($ch, CURLOPT_POST, 1); 
$result = curl_exec ($ch); 

echo $result;  
curl_close($ch);

What am I doing wrong. After this is working I want to redirect to another page and get content from my site.

A: 

Your $url should be that of gmail login, not you own script.

Majid
That is the login page. It's where the login form is located. I'm not trying to login to Gmail with this script.
Pjack
So, why are you accessing your own login page with curl? You can access it with an ordinary form and pass in the username and password, then in the submit handler put curl code to use that data to login to gmail on behalf of you.
Majid
"What I'm trying to do is login to a remote site, by having curl do the login form and then send back that it was successful."What be this "remote site" then?
m3n
+1  A: 

View the source of the login page. Look for the form HTML tag. Within that tag is something that will look like action= Use that value as $url, not the form itself.

Also, while you are there, verify the input boxes are named what you have them listed as.

For example, a basic login form will look similar to:

<form method='post' action='postlogin.php'>
    Email Address: <input type='text' name='email'>
    Password: <input type='password' name='password'>
</form>

Using the above form as an example, change your value of $url to

$url="http://www.myremotesite.com/postlogin.php";

verify the values you have listed in

`$postdata = "email=".$username."&password=".$password;`

and it should work just fine.

Joseph
A: 

Hey Pjack! Did you get the answer to this question? I am having the same problem and my code is only showing the main page of the website. If you got the answer, could you tell me how to resolve this problem?

ravenarvind