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 = 'http://example.com/get-the-post-data.php';
$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);
curl_close($ch);
//check the result
var_dump($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.

WPOven

With Plans Starting Just $16

© 2024 WPOven Inc. All rights reserved. WPOven® and WordPress® are registered trademarks.
WPOven is the best Managed WordPress hosting for agencies and businesses that want to succeed. With Unlimited sites hosting option, its easy to use and manage, so you can focus on growing your business. 
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram