Skip to main content

HTTP POST without cURL using PHP



I don't think we do a very good job of evangelizing some of the nice things that the PHP streams layer does in the PHP manual, or even in general. At least, every time I search for the code snippet that allows you to do an HTTP POST request, I don't find it in the manual and resort to reading the source. (You can find it if you search for "HTTP wrapper" in the online documentation, but that's not really what you think you're searching for when you're looking). 

So, here's an example of how to send a POST request with straight up PHP, no cURL:

<?php
     function do_post_request($url, $data, $optional_headers = null) {
          $params = array('http' => array(
                                'method' => 'POST',
                               'content' => $data
                         ));
         if ($optional_headers !== null) {
              $params['http']['header'] = $optional_headers;
          }
          $ctx = stream_context_create($params);
          $fp = @fopen($url, 'rb', false, $ctx);
          if (!$fp) {
                  throw new Exception("Problem with $url, $php_errormsg");
          }
         $response = @stream_get_contents($fp);
         if ($response === false) {
           throw new Exception("Problem reading data from $url, $php_errormsg");
          }
         return $response;
}
$optional_headers is a string containing additional HTTP headers that you would like to send in your request.
PHP's HTTP wrapper will automatically fill out the Content-Length header based on the length of the $data that you pass in. It will also automatically set the Content-Type to application/x-www-form-urlencoded if you don't specify one in the $optional_headers.
I find this very handy; I don't need to code in redirection logic, HTTP auth handling, user agent setting and so on; they are handled for me by PHP. This works for HTTPS as well, if you have openssl enabled.
You may also want to look into http_build_query() which is a convenience function that allows you to assemble query/post parameters from a PHP variable, applying appropriate escaping. You can find an example of this in the REST helper below.
Kudos to Sara Golemon for both http_build_query and exposing the HTTP context parameters up to userspace.

A Generic REST helper
Many web services offer a REST-ful interface for consuming their data, using GET requests for information retrieval and POST requests for making changes. Below you'll find a helper function that can very easily be used to consume a REST API.

The $url parameter is the HTTP or HTTPS URL for the web service. $params is an associative array of form parameters to pass to the web service; they will be passed as _GET parameters for GET requests or _POST parameters for POST requests. The $verb parameter can be GET or POST (and presumably any other valid HTTP REQUEST verb, such as PUT or DELETE, although I haven't tried those and can't say whether they will work as expected). The $format parameter can be "json" or "xml" and will automatically return a decoded json or XML document, respectively.

I've used simplexml here because it is... simple. You could very easily add a "dom" format to return the object using the richer and more complex DOM API instead.

This function uses the ignore_errors context parameter. Without this set (the default is false), PHP will treat 400 and 500 HTTP status codes as a failure to open the stream and won't return you any data. This is usually what you want when using fopen or file_get_contents, but REST services tend to set the HTTP status to indicate the error and will usually send back a payload that describes the error. We turn on ignore_errors so that we treat any returned payload as json or xml.

When using POST with REST, take care: PHP's HTTP redirection handler will drop your POST payload if the endpoint issues a redirect. If you experience problems using POST with the function below, it might be due to redirects. Most of the POST calls I've run into issue redirects if the URL is missing a trailing '/' character. In other words, if you experience problems where it seems like your parameters are not being sent in, try appending a '/' to the end of the URL and try it again.

<?php
 function rest_helper($url, $params = null, $verb = 'GET', $format = 'json') {
       $cparams = array( 'http' => array( 'method' => $verb,
                                  'ignore_errors' => true ) );
        if ($params !== null) {
              $params = http_build_query($params);
               if ($verb == 'POST') {
                     $cparams['http']['content'] = $params;
                 } else {
                        $url .= '?' . $params;
                }
          }
          $context = stream_context_create($cparams);
          $fp = fopen($url, 'rb', false, $context);
           if (!$fp) {
                 $res = false;
            } else {
               // If you're trying to troubleshoot problems, try uncommenting the
              // next two lines; it will show you the HTTP response headers across
              // all the redirects:
              // $meta = stream_get_meta_data($fp);
              // var_dump($meta['wrapper_data']);
               $res = stream_get_contents($fp);
            }
            if ($res === false) {
                  throw new Exception("$verb $url failed: $php_errormsg");
             }
             
              switch ($format) {
                  case 'json':
                            $r = json_decode($res);
                            if ($r === null) {
                                 throw new Exception("failed to decode $res as json");
                             }
                                     return $r;
                  case 'xml':
                             $r = simplexml_load_string($res);
                             if ($r === null) {
                                 throw new Exception("failed to decode $res as xml");
                             }
                             return $r;
               }
               return $res;
      }
      // This lists projects by Ed Finkler on GitHub:

    foreach ( rest_helper('http://github.com/api/v2/json/repos/show/funkatron') ->repositories as $repo) {
                echo $repo->name, "\n";
                echo htmlentities($repo->description), "\n";
                echo "\n";
       } // This incomplete snippet demonstrates using POST with the Disqus API

       var_dump(
             rest_helper( "http://disqus.com/api/thread_by_identifier/",
                        array(
                                  'api_version' => '1.1',
                                  'user_api_key' => $my_disqus_api_key,
                                  'identifier' => $thread_unique_id,
                                  'forum_api_key' => $forum_api_key,
                                  'title' => 'HTTP POST from PHP, without cURL', ),
                       'POST' ) );
You can find more documentation on the HTTP wrapper options in the HTTP and HTTPS page in the PHP manual, more on the GitHub API at github.com, more on the Disqus API and more on Ed Finkler at his blog.

Original Post

Comments

Popular posts from this blog

Survey says: PHP passes Microsoft Active Server Pages

By JT Smith on June 11, 2002 (8:00:00 AM) With a faltering economy forcing companies to cut spending whenever possible, less expensive and freely available Open Source software solutions may be gaining in popularity. Those wanting proof can look no further to PHP taking the top server-side scripting spot in a recent Internet host survey. In April 2002, Netcraft's monthly Web server survey revealed that 24 percent, or around 9 million of the 37 million sites it surveyed, were using Hypertext Preprocessor (PHP) for a server side scripting language. For the first time, an Open Source scripting solution had passed Microsoft's proprietary Active Server Pages scripting to claim the top spot on the Netcraft survey. For both the April and May Netcraft surveys, PHP and ASP were almost too close to call, with Microsoft's product offering coming in just a hair under 24 percent of all hosts running a server-side script...

PHP script to upload file securely

How to Write a Secure PHP Script for File Uploads File uploads are a common feature in web applications, but they can introduce significant security risks if not handled properly. In this article, we'll walk through the steps to securely upload files to a server using PHP. We'll cover key security measures such as file validation, limiting file types, setting file size limits, and managing file storage. We will also create reusable functions to handle the upload process. 1. Create the HTML Form First, we need an HTML form that allows users to select and upload a file. Ensure that the form uses the POST method and includes the enctype="multipart/form-data" attribute. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Secure File Upload</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/...

The 7 Types of Leadership: Inspiring Examples and Insights

Leadership is a multifaceted concept with various styles that cater to different needs and situations. Understanding these styles can help you develop your leadership skills and adapt to diverse scenarios. In this article, we'll explore seven prominent leadership styles, each accompanied by insightful examples from influential authors. Let's dive in! Type 1: Transformational Leadership 🚀 Transformational Leadership: Inspiring Change and Innovation Transformational leadership focuses on inspiring and motivating followers to achieve extraordinary outcomes and, in the process, develop their own leadership capacity. Example from Author: James MacGregor Burns James MacGregor Burns, in his book "Leadership," describes transformational leaders as those who seek to change the status quo by appealing to their followers' values and sense of higher purpose. 🔸 Characteristics: - Inspirational Motivation - Intellectual Stimulation - Individualized Consideration - Idealized I...