1

I have 2 files below, and I'm trying to use cURL to automatically log in and get the message "login success", as oppose to the message "login fail". How would I do that?

I know this may look like I'm begging for others to write my codes for me, and that I didn't put any effort into researching cURL, and making my own cURL code, before asking this question. But, I've actually done a lot of researching, but I wasn't able to find any examples that I understanded, or does what I wanted it to do, so that's why I'm asking here. Please help if you can.

test1.php

<form method = "POST" action = "test2.php">
<input type = "username" name = "username">
<input type = "password" name = "password">
<input type = "submit" value = "submit">
</form>

test2.php

$username = $_POST['username'];
$password = $_POST['password'];

if ($username == "username" && $password == "password") { 
echo "login success";
}
else {
echo "login fail";
}

Need Result:

login success
jessica
  • 1,667
  • 1
  • 17
  • 35
  • You just need to put the username and password into `CURLOPT_POSTFIELDS`. What problem are you having with that? – Barmar Oct 28 '15 at 19:15
  • Try something,stumbling through things is called learning.If you just get the code from an answer you`ll remember nothing – Mihai Oct 28 '15 at 19:19
  • @Barmar I'm sorry. Please provide an example of that, using the codes above. I'll understand better that way. – jessica Oct 28 '15 at 19:20
  • @Mihai I don't know where to begin. All the examples I saw were too complicated, and none of them does what I need for it to do above. – jessica Oct 28 '15 at 19:24
  • @Mihai do you put cUrl in between php tags? what tags do you put it in between? – jessica Oct 28 '15 at 19:29
  • @Barmar do you put curl in between php tags? – jessica Oct 28 '15 at 19:29
  • It's the arguments to `curl_setopt`. Do you understand the basics of using `cURL` from PHP? Maybe that's where you should start. If I just post the code, you're not going to understand it without that. – Barmar Oct 28 '15 at 19:31
  • Here's a web site with an explanation of how to submit a form from PHP. It explains every step in detail. http://www.html-form-guide.com/php-form/php-form-submit.html – Barmar Oct 28 '15 at 19:34
  • @Barmar I'm reading the link you sent me right now, but please also post the code as an example. You don't have to explain it or anything. It would help me a lot, please. – jessica Oct 28 '15 at 19:41

3 Answers3

1

Here's the basic code:

<?php
$ch = curl_init('http://yourdomain.com/test2.php');
$post_params = array('username' => 'username', 'password' => 'password');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); // PHP will format the array into POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response == 'login success') {
    echo 'Login was successful!';
} else {
    echo 'Something went wrong :(';
}

Note that logging into a real web site is more complicated than just sending the form fields. You also need to get back the session cookie, and send that on future requests. See

Displaying Logged in Content on a Website

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • It comes up with an error message: Warning: curl_setopt() expects parameter 2 to be long, string given Warning: curl_setopt() expects exactly 3 parameters, 2 given – jessica Oct 28 '15 at 20:00
  • Sorry, I had a typo in the first line, and I left out an argument in the second line. Fixed now. – Barmar Oct 28 '15 at 20:02
  • It echo 'something went wrong :(, and when I substituted that with $response, it echo out the two input fields, and the submit button. – jessica Oct 28 '15 at 20:06
  • Is `test2.php` the script that contains the form? The returned data contains the form in it. – Barmar Oct 28 '15 at 20:09
  • All the codes in test2.php are shown above. I don't have any other codes in it. – jessica Oct 28 '15 at 20:09
  • test1.php is the script that contains the form. test2.php is the php that returns 'success' or 'fail' after you enter the username, and password in test1.php and press the submit button. – jessica Oct 28 '15 at 20:10
  • Then I don't see any way that `$response` could contain the form fields. Are you sure you have `test2.php` in the `curl_init` line, not `test1.php`? – Barmar Oct 28 '15 at 20:11
  • Ah. I had test1.php in the curl_init, because I thought that's what it needs to access. If we need to login to a external site, how do we figure out what the url of their test2.php are, since only their test1.php are shown to us? – jessica Oct 28 '15 at 20:16
  • Look at the source, to see the `action="URL"` part of the form. That's where you have to send the curl. – Barmar Oct 28 '15 at 20:19
  • Note that really logging into a web site is more complicated than just sending the form fields. When you login to a web site, it uses session variables to remember that you're logged in, and that depends on cookies. So you need to read the cookies that the server sends, and then send them back on future requests. – Barmar Oct 28 '15 at 20:20
  • And to see the name of their input types, just search for input types, right? – jessica Oct 28 '15 at 20:22
  • Yes. Look for ``, ` – Barmar Oct 28 '15 at 20:23
  • Also, yes, that sounds complicated. Do you have any more easy to understands sites that talk about using cookies and session variables? – jessica Oct 28 '15 at 20:23
  • I just added a link to a question about it to my answer. However, it sounds like you're not advanced enough for any of this to be "easy to understand". – Barmar Oct 28 '15 at 20:24
  • Thanks, for all your help. You helped me a lot today. I appreciates it! :) – jessica Oct 28 '15 at 20:30
0

you're looking for the cURL command-line syntax to do a POST request:

curl -X POST --data "param1=value1&param2=value2" https://example.com/test2.php

Community
  • 1
  • 1
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • I'm sorry. I don't understand. Where exactly, do I put that code? And what other codes are necessary for that to work? Please provide a full example, that gets the message "login success". It would really help me a lot. – jessica Oct 28 '15 at 19:23
  • Do you put cURL in between php tags? What tags do you put it in betwee? – jessica Oct 28 '15 at 19:28
  • His question is about using cURL from PHP, not a shell script. – Barmar Oct 28 '15 at 19:30
  • @Barmar that's unclear from the question text as originally posted, but I think you're right. – Dan O Oct 28 '15 at 19:31
0

I think you can do something like this:

$data = array('username' => 'username', 'password' => password);
$url = 'http://example.com';

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
var_dump($response);

You can see more details over in the accepted answer over on Passing $_POST values with cURL

Community
  • 1
  • 1
Andrew Cavanagh
  • 277
  • 2
  • 14