0

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?

2 Answers 2

2

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

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

Comments

1

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'
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.