Can anyone show me how to do a PHP cURL with an HTTP POST?
I want to send data like this:
username=user1, password=passuser1, gender=1
To www.example.com
I expect the cURL to return a response like result=OK
. Are there any examples?
PHP cURL is a library that is the most powerful extension of PHP. It allows the user to create the HTTP requests in PHP. cURL library is used to communicate with other servers with the help of a wide range of protocols. cURL allows the user to send and receive the data through the URL syntax.
HTTP POST - Everything curl. HTTP POST. POST is the HTTP method that was invented to send data to a receiving web application, and it is how most common HTML forms on the web works. It usually sends a chunk of relatively small amounts of data to the receiver.
cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.
<?php // // A very simple PHP example that sends a HTTP POST to a remote site // $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3"); // In real life you should use something like: // curl_setopt($ch, CURLOPT_POSTFIELDS, // http_build_query(array('postvar1' => 'value1'))); // Receive server response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec($ch); curl_close ($ch); // Further processing ... if ($server_output == "OK") { ... } else { ... } ?>
// set post fields $post = [ 'username' => 'user1', 'password' => 'passuser1', 'gender' => 1, ]; $ch = curl_init('http://www.example.com'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // execute! $response = curl_exec($ch); // close the connection, release resources used curl_close($ch); // do anything you want with your response var_dump($response);
<?php // mutatis mutandis namespace MyApp\Http; class CurlPost { private $url; private $options; /** * @param string $url Request URL * @param array $options cURL options */ public function __construct($url, array $options = []) { $this->url = $url; $this->options = $options; } /** * Get the response * @return string * @throws \RuntimeException On cURL error */ public function __invoke(array $post) { $ch = \curl_init($this->url); foreach ($this->options as $key => $val) { \curl_setopt($ch, $key, $val); } \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, true); \curl_setopt($ch, \CURLOPT_POSTFIELDS, $post); $response = \curl_exec($ch); $error = \curl_error($ch); $errno = \curl_errno($ch); if (\is_resource($ch)) { \curl_close($ch); } if (0 !== $errno) { throw new \RuntimeException($error, $errno); } return $response; } }
// create curl object $curl = new \MyApp\Http\CurlPost('http://www.example.com'); try { // execute the request echo $curl([ 'username' => 'user1', 'password' => 'passuser1', 'gender' => 1, ]); } catch (\RuntimeException $ex) { // catch errors die(sprintf('Http error %s with code %d', $ex->getMessage(), $ex->getCode())); }
Side note here: it would be best to create some kind of interface called AdapterInterface
for example with getResponse()
method and let the class above implement it. Then you can always swap this implementation with another adapter of your like, without any side effects to your application.
Usually there's a problem with cURL in PHP under the Windows operating system. While trying to connect to a https protected endpoint, you will get an error telling you that certificate verify failed
.
What most people do here is to tell the cURL library to simply ignore certificate errors and continue (curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
). As this will make your code work, you introduce huge security hole and enable malicious users to perform various attacks on your app like Man In The Middle attack or such.
Never, ever do that. Instead, you simply need to modify your php.ini
and tell PHP where your CA Certificate
file is to let it verify certificates correctly:
; modify the absolute path to the cacert.pem file curl.cainfo=c:\php\cacert.pem
The latest cacert.pem
can be downloaded from the Internet or extracted from your favorite browser. When changing any php.ini
related settings remember to restart your webserver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With