3

Trying some simple code to work with cURL but no lucky.. I tried to POST a form but nothing happens and no error occurs.

Here is my code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/curl/home2.php');
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'usuario=teste&senha=12345');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . 
                curl_error($ch);

echo $store;

When submit happens on the form home2.php it should go to another page and return some data. Am i doing something wrong?

--- UPDATE

Here is my simple home2.php code:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <form method="POST" action="login.php" id="formLogin" name="formLogin">
        Usuario <input name="login" type="text">
        Senha <input name="senha" type="text">
        <input type="submit">
    </form>
</body>
</html>

login.php :

<?php

$login = $_POST['login'];
$senha = $_POST['senha'];

if($login == "teste"){
    echo "OK";
}

?>

It should be "OK" as my result, right?

9
  • 1
    It looks OK for this code. Can you show that full code including home2.php? Commented Jul 26, 2015 at 5:02
  • Try to add these 2 lines before curl_exec curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); Commented Jul 26, 2015 at 6:12
  • @spicydog Done. Updated the post. Commented Jul 26, 2015 at 13:44
  • @Mihai Same thing :( Commented Jul 26, 2015 at 13:45
  • Your error seems fairly obvious. You're sending the curl request with a "username" key named usuario, but your form is expecting login. You also need to submit towards login.php and not home2.php. What actually happens when you submit the form on home2.php through a browser is that the browser sends the form input to login.php - it never gets sent to home2.php. Commented Jul 26, 2015 at 14:26

2 Answers 2

2

Your form on home2.php looks like this:

<form method="POST" action="login.php" id="formLogin" name="formLogin">
    Usuario <input name="login" type="text">
    Senha <input name="senha" type="text">
    <input type="submit">
</form>

It's important to note that the username input box is named login, yet when you create the cURL request, you're sending the username as usario. This needs to be changed. You also need to emulate what a browser would do and send the post variables towards login.php (action="login.php") instead of home2.php (this page only includes the form, and never receives any of the input form data).

All in all, your code should be fixable by:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:8888/curl/login.php');
// Change the URL                                         ^^^^^
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'login=teste&senha=12345');
// Change the username key             ^^^^^
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);

print_r(curl_getinfo($ch));
echo curl_errno($ch) . '-' . 
                curl_error($ch);

echo $store;

If you're ever in doubt, the network tab in your browser (or any HTTP packet sniffer) will tell you exactly what requests the browser makes, with what variables, towards what URLs, and you'll be able to replicate it way easier.

Note: If your success.php page that login.php apparently redirects to on success does anything at all, you'll also need to have this be called automatically.

If login.php automatically redirects, you can just use the cURL option CURLOPT_FOLLOWLOCATION set to 1.

Sign up to request clarification or add additional context in comments.

9 Comments

It worked. So i'm trying to do with some site that i need and it's not mine (Not localhost). But am facing an problem that also don't POST and i'm using the right name as my input. What changes is the form point to something like that: index.php#tabs-1. And even if i set this to my URL, the POST do not happen. Do you think i should open another Question to be more specific?
If the only difference is the hashtag, then there's some javascript fiddling going on. Use an HTTP sniffer to see what requests the site is making when clicking on the button you want to emulate.
Using what you said and going to the view source i found it. It was a hidden field. Now it's working. - Sorry to keep bothering you but what if I want to make like a 100 requests each time and grab the returned data. Easy task? I mean, the result is a full webpage and i want part of the fields. I don't know what to search.. "cURL grab data"? @h2ooooooo
You're looking for a "web scraper" or a "dom parser". Essentially you'd want to search for "php web scraper" or "php dom parser". I like to use simplehtmldom to give me a jQuery like search-and-collect. phpQuery is also quite decent.
I have to use as a complement to what i've done, right? Please say yes. Because i saw an usage like: "$html = file_get_html('google.com/');" and got scared. I can use like html = file_get_html($store); ?
|
1

You mentioned "When submit happens on the form home2.php it should go to another page". By that do you mean that there is an HTTP redirect? If so, you need to tell cURL to follow redirects. Like this:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

1 Comment

When submit happens it should go to login.php as set in the action form in home2.php. I updated the post with full code. Also tried what you suggested but still the same. @Asaph

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.