How to Pass $_POST Variables via PHP CURL

php curl post request with headers

If you wish to pass PHP POST array variables without submitting a form, using the PHP Curl library to another URL, you first need to have a an Array which holds all the values for the POST array

$post['lname'] = 'Last name';
$post['fname'] = 'First Name'
$post['mail'] = '[email protected]';

Once we have the array set, we create the variables for the destination URL and POST fields

$url = '';
$fields = array ('lname' =>urlencode($post['lname'],
'fname' = urlencode($post['fname']),
'mail' = urlencode($post['mail']));

Now we need a variable with all the post fields in the format which can be sent through curl

foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

Now we start the Curl Connection and send the post data through the curl request

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
//check the result

We are sure you will love what WPOven has to offer. We offer FREE WP Dev & Server Support, Get dedicated servers also host unlimited websites. We offer FREE SSD, SSL, Business Emails, FREE offsite backups & daily malware scanning, cleanup.


With Plans Starting Just $16

© 2022 WPOven Inc. All rights reserved. WPOven® and WordPress® are registered trademarks.
WPOven INC is more than just a managed WordPress Hosting that offers more secured unlimited website hosting with it’s CDN ready worldwide Datacentres.
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram