Ad

How To Submit Custom POST Form Via CURL PHP In Wordpress

- 1 answer

I'm making a theme wordpress use childtheme in localhost. I had a basic form html in footer.php:

<form method="post">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit">Subscribe</button>
</form>

Now , I want to submit form to an api url via cURL php. I know code to set up cURL like this:

$data =  $_POST;

$curl = curl_init();

$options =array(
    CURLOPT_RETURNTRANSFER =>true,
    CURLOPT_URL => 'link_to_url',
    CURLOPT_POST => true,
    CURLOPT_CONNECTTIMEOUT => 30,
    CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POSTFIELDS => http_build_query($data)
);
curl_setopt_array($curl, $options);

$result = curl_exec($curl);
curl_close($curl);

I want to when form is submitted , POST data will send via this cURL, But I don't know where I should place this code and how to process it. I think it maybe placed in a function in function.php but I don't know how to write it .Please help me!

Ad

Answer

If you wish to execute said code within the same file can do something like this:

<?php
// any other php code above
if(isset($_POST["infoForm"])) {
    $curl = curl_init();

    $data = array(
        'post_request_name_for_fName' => $_POST["FNAME"],
        'post_request_name_for_lName' => $_POST["FNAME"],
        'post_request_name_for_email' => $_POST["EMAIL"]       
     );

    $options =array(
        CURLOPT_RETURNTRANSFER =>true,
        CURLOPT_URL => 'link_to_url',
        CURLOPT_POST => true,
        CURLOPT_CONNECTTIMEOUT => 30,
        CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_POSTFIELDS => http_build_query($data)
    );
    curl_setopt_array($curl, $options);

    $result = curl_exec($curl);
    curl_close($curl);
}
// any other php code below
?>

and set the HTML to this:

<form method="post">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit" name="infoForm">Subscribe</button>
</form>

Now here's the explanation behind it. You set the name of your submit button to infoForm (or whatever else you want it to be) so the PHP can know which form is posted and when. You then use if(isset($_POST["infoForm"])) to check whether the form was posted, and then you execute the lower code to post it.

The $_POST["FNAME"] and other variants check the data found in each one of the inputs and sets that to a value of the $data array. Then you can change all the values of post_request_name_for_fName to whatever the values for your post request need it to be (i.e.; if you're post request needs a 'name' and 'email' parameter, just change the values above as needed).

If you also want to transition to a different page you can make a new file (i.e.; post.php) and add the code in their without the if(isset), and set your html as this:

<form method="post" action="post.php">     
    <input name="FNAME" type="text">
    <input name="LNAME" type="text">
    <input name="EMAIL" type="email">
    <button type="submit" name="infoForm">Subscribe</button>
</form>
Ad
source: stackoverflow.com
Ad