Patrick Groozder
Patrick Groozder

Reputation: 65

Send and receive data form CURL

I have simply script:

<?php if($_POST){
    if($_POST['captcha'] == 'random'){ ?>
<div id="answer">
    <?php echo $_POST['captcha'] . uniqid(); ?>
</div>
<?php
    }

} ?>

<form name="contactform" method="post" action="">
    email: <input  type="text" name="email"> <br />
    ###IMAGE###: random <br />
    captcha: <input  type="text" name="captcha">
    <input type="submit" value="Submit">
</form>

captcha is random, but for this test i use always 'random'. This script working OK.

So - i would like open this site and fill in captcha and submit form and receive data from DIV #answer.

I create:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);

print_r($output);

This show me form, but if i fill in captcha and submit form then this not handles $_POST.

How can i make it? I must first fill captcha.

Upvotes: 1

Views: 3253

Answers (2)

Abid Hussain
Abid Hussain

Reputation: 7762

try this:-

$email = $_POST["email"];
$pass = $_POST["pass"];
$captcha = $_POST["captcha"];
$curlPost = array('email' => $email, 'password'=>'$pass', 'captcha'=>$captcha);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);

echo "<pre>";
print_r($data);

Upvotes: 2

user745235
user745235

Reputation:

You forgot to send the data

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

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/script.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_POST, 1); 

$output = curl_exec($ch);
curl_close($ch);

print_r($output);

Upvotes: 2

Related Questions