This is a short PHP tutorial on how to send a HTTP POST request without using cURL. This may be helpful for those of you that require an easy alternative to PHP’s cURL extension.
Custom POST function.
Let’s create a customĀ post function that doesn’t use any of the cURL functions:
/** * Send a POST request without using PHP's curl functions. * * @param string $url The URL you are sending the POST request to. * @param array $postVars Associative array containing POST values. * @return string The output response. * @throws Exception If the request fails. */ function post($url, $postVars = array()){ //Transform our POST array into a URL-encoded query string. $postStr = http_build_query($postVars); //Create an $options array that can be passed into stream_context_create. $options = array( 'http' => array( 'method' => 'POST', //We are using the POST HTTP method. 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postStr //Our URL-encoded query string. ) ); //Pass our $options array into stream_context_create. //This will return a stream context resource. $streamContext = stream_context_create($options); //Use PHP's file_get_contents function to carry out the request. //We pass the $streamContext variable in as a third parameter. $result = file_get_contents($url, false, $streamContext); //If $result is FALSE, then the request has failed. if($result === false){ //If the request failed, throw an Exception containing //the error. $error = error_get_last(); throw new Exception('POST request failed: ' . $error['message']); } //If everything went OK, return the response. return $result; }
Take some time to read through the comments in the code above, as these explain how the POST request is constructed and sent via file_get_contents.
How to use the post function.
The custom function above takes in two parameters:
- A URL string. This is the URL that we want to send a POST request to.
- An associative array containing the POST variables that we want to send.
Here is an example of the function being used:
//Send a POST request without cURL. $result = post('http://example.com', array( 'foo' => 'bar', 'name' => 'Wayne' )); echo $result;
In the PHP code above, we are sending a POST request to example.com. There are two post variables: These are calledĀ foo and name.
Failed requests and error handling.
Note that our custom post function will throw an Exception if the HTTP request fails. This means that you can catch any failed POST requests like so:
try{ $result = post('http://google.com', array( 'foo' => 'bar', 'name' => 'Wayne' )); echo $result; } catch(Exception $e){ echo $e->getMessage(); }
If you run the code above, you will be met with the following output:
POST request failed: file_get_contents(http://google.com): failed to open stream: HTTP request failed! HTTP/1.0 405 Method Not Allowed
This 405 error occurs because Google does not accept POST requests.
Note: If you do have access to PHP’s cURL functions, then I would suggest that you use them. The cURL functions come with a lot of different options, many of which are extremely useful.
Hopefully, you found this alternative to be useful!