My goal is to have a WordPress form that when posting, send the input to an API into an ERP.
Right now it seems that the values are empty when sending the way I do.
In CURLOPT_POSTFIELDS if instead of “$inputname” I enter something like “Martin” it works perfectly.
How can I get the value of the 4 fields I want to send? I tried $POST[‘inputname’] but same, doesn’t work.
I’m not an expert at all in php, so any tips would be appreciated! Thanks!
<?php if($_POST['Envoyer']) { ?> <form id="formid2" action="" method="POST"> <input type="submit" name="Envoyer2" value="Recommencer" /> </form> <? $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "*******", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS =>"{rn "FirstName": "$inputname",rn "Name": "$inputsurname",rn "PhoneNo": "$inputphone",rn "No": "$inputcomment"rn}", CURLOPT_HTTPHEADER => array( "Authorization: Basic ************", "Content-Type: application/json", "Cookie: RuntimeTenantAffinity=*******" ), )); $response = curl_exec($curl); curl_close($curl); echo $INPUTN; } else { ?> <form id="formid" action="" method="POST"> <p>Nom : <input type="text" name="inputname" value="" /></p> <p>Prénom : <input type="text" name="inputsurname" value="" /></p> <p>Téléphone : <input type="text" name="inputphone" value="" /></p> <p>Commentaire : <input type="text" name="inputcomment" value="" /></p> <input type="submit" name="Envoyer" value="Envoyer" /> </form> <? } ?>
Advertisement
Answer
You should not create json
manually, it would create errors if some special character are there in user inputs.
Create an array and then use json_encode
to pass data in your curl. Also for POST
request don’t use CUSTOMREQUEST
options instead use CURLOPT_POST
.
// User inputs are $inputname, $inputsurname, $inputphone, $inputcomment // Create an array $arrData = [ 'FirstName' => $inputname, 'Name' => $inputsurname, 'PhoneNo' => $inputphone, 'No' => $inputcomment ]; // Now pass it in curl options array [ ... CURLOPT_POST => 1, CURLOPT_POSTFIELDS => json_encode($arrData), ]
You can also pass data in array if api supports it. Here are options that you can use.
The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ‘;type=mimetype’. This parameter can either be passed as a urlencoded string like ‘para1=val1¶2=val2&…’ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.
Finally strongly suggest you to use error check for curl so you can know what’s going wrong.
if(curl_exec($curl) === false){ echo 'Curl error: ' . curl_error($curl); } else{ echo 'Operation completed without any errors'; }