frank allen
frank allen

Reputation: 35

php curl doesn't send post data in form

I'm new in using cURL, so I need your help know. My situation is, that I would like to send a form (formular.php) via a form-site (form.php).

The form.php calls the formular.php via cURL.

Here are both source codes:

form.php

<?php
    error_reporting(-1);
    $url = 'http://localhost/formular.php';
    $data = array('customerline' => 'nummer1', 'customerphonenumber' => 'nummer2');
    $postString = http_build_query($data, '', '&');
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_POST, 2); 
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $postString); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    $post = curl_exec ($ch); 
    curl_close ($ch); 
    echo $post;
?>

formular.php

<?php if(isset($_POST['form'])): ?>
    Sent!
<?php else: ?>
    <form action="http://localhost/formular.php" method="post" name="form">
        <input type="text" name="customerline" />
        <input type="text" name="customerphonenumber" />
        <input type="submit" value="send" name="form" />
    </form>
<?php endif; ?>

My problem is, that I doesn't understand how to get the text passage "Sent!".

Could you help me?

Upvotes: 0

Views: 2662

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39443

You are expecting on your if condition to have a value against the form if(isset($_POST['form'])) but from curl you are not sending it.

You should add that. So, your post parameter should be:

$data = array(
   'customerline' => 'nummer1',
   'customerphonenumber' => 'nummer2',
   'form' => 'value'
);

Upvotes: 1

Adam Heath
Adam Heath

Reputation: 4763

You can just pass in the array directly without converting it to a query string. Also CURLOPT_POST should be true.

Try:

$url = 'http://localhost/formular.php';
$data = array('customerline' => 'nummer1', 'customerphonenumber' => 'nummer2');
$ch = curl_init(); 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
$post = curl_exec ($ch); 
curl_close ($ch); 
echo $post;

More info on options https://www.php.net/curl_setopt

Upvotes: 2

Related Questions