Zebra_cURL::post PHP Method

post() public method

This method will automatically set the following options: - CURLINFO_HEADER_OUT - TRUE - CURLOPT_HEADER - TRUE - CURLOPT_NOBODY - FALSE - CURLOPT_POST - TRUE - CURLOPT_POSTFIELDS - the POST data ...and will unset the following options: - CURLOPT_BINARYTRANSFER - CURLOPT_CUSTOMREQUEST - CURLOPT_HTTPGET - TRUE - CURLOPT_FILE Multiple requests are processed asynchronously, in parallel, and the callback function is called for each and every request, as soon as a request finishes. The number of parallel requests to be constantly processed, at all times, can be set through the {@link threads} property. See also the {@link pause_interval} property. Note that requests may not finish in the same order as initiated! the callback function to be executed for each and every request, as soon as a request finishes the callback function receives as argument an object with 4 properties (info, header, body and response) function mycallback($result) { everything went well at cURL level if ($result->response[1] == CURLE_OK) { if server responded with code 200 (meaning that everything went well) see http://httpstatus.es/ for a list of possible response codes if ($result->info['http_code'] == 200) { see all the returned data print_r('
');
            print_r($result);
show the server's response code
        } else die('Server responded with code ' . $result->info['http_code']);
something went wrong
($result still contains all data that could be gathered)
    } else die('cURL responded with: ' . $result->response[0]);

}
include the Zebra_cURL library
require 'path/to/Zebra_cURL';
instantiate the Zebra_cURL object
$curl = new Zebra_cURL();
do a POST and execute the "mycallback" function for each
request, as soon as it finishes
$curl->post(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
    ),
), 'mycallback');
note that we're also uploading a file this time
and note that we're prefixing the file name with @
$curl->post(array(
    'http://www.somewebsite.com'  =>  array(
        'data_1'  =>  'value 1',
        'data_2'  =>  'value 2',
        'data_3'  =>  '@absolute/path/to/file.ext',
), 'mycallback');
        
public post ( mixed $urls, mixed $callback = '' ) : void
$urls mixed An associative array in the form of url => post-data, where "post-data" is an associative array in the form of name => value. "post-data" can also be an arbitrary string - useful if you want to send raw data (like a JSON) To post a file, prepend the filename with @ and use the full server path. For PHP 5.5+ files are uploaded using {@link http://php.net/manual/ro/class.curlfile.php CURLFile} and {@link https://wiki.php.net/rfc/curl-file-upload CURLOPT_SAFE_UPLOAD} will be set to TRUE. For lower PHP versions, files will be uploaded the "old" way and the file's mime type should be explicitly specified by following the filename with the type in the format ';type=mimetype'. as most of the times cURL will send the wrong mime type... The Content-Type header will be set to multipart/form-data. @param mixed $callback (Optional) Callback function to be called as soon as a request finishes. May be given as a string representing a name of an existing function, as an anonymous function created on the fly via {@link http://www.php.net/manual/en/function.create-function.php create_function} or via a {@link http://www.php.net/manual/en/function.create-function.php closure}. The callback function receives as first argument an object with 4 properties as described below, while any further arguments passed to the {@link post} method will be passed as extra arguments to the callback function: - info - an associative array containing information about the request that just finished, as returned by PHP's {@link http://php.net/manual/en/function.curl-getinfo.php curl_getinfo} function; - headers - an associative array with 2 items: - last_request an array with a single entry containing the request headers generated by the last request; so, remember, if there are redirects involved, there will be more requests made, but only information from the last one will be available; if explicitly disabled via the {@link option} method by setting CURLINFO_HEADER_OUT to 0 or FALSE, this will be an empty string; - responses an array with one or more entries (if there are redirects involved) with the response headers of all the requests made; if explicitly disabled via the {@link option} method by setting CURLOPT_HEADER to 0 or FALSE, this will be an empty string; Unless disabled, each entry in the headers' array is an associative array in the form of property => value - body - the response of the request (the content of the page at the URL). Unless disabled via the {@link __construct() constructor}, all applicable characters will be converted to HTML entities via PHP's {@link http://php.net/manual/en/function.htmlentities.php htmlentities} function, so remember to use PHP's {@link http://www.php.net/manual/en/function.html-entity-decode.php html_entity_decode} function to do reverse this, if it's the case; If "body" is explicitly disabled via the {@link option} method by setting CURLOPT_NOBODY to 0 or FALSE, this will be an empty string; - response - the response given by the cURL library as an array with 2 entries: the first entry is the textual representation of the result's code, while second is the result's code itself; if the request was successful, these values will be array(CURLE_OK, 0); consult {@link http://www.php.net/manual/en/function.curl-errno.php#103128 this list} to see the possible values of this property; If the callback function returns FALSE while {@link cache} is enabled, the library will not cache the respective request, making it easy to retry failed requests without having to clear all cache. @return void
$callback mixed
return void
    public function post($urls, $callback = '')
    {
        // if "urls" argument is not an array, trigger an error
        if (!is_array($urls)) {
            trigger_error('First argument to "post" method must be an array!', E_USER_ERROR);
        }
        // iterate through the list of URLs to process
        foreach ((array) $urls as $url => $values) {
            // if $values is an array
            if (is_array($values)) {
                // walk recursively through the array
                array_walk_recursive($values, function (&$value) {
                    // if we have to upload a file
                    if (strpos($value, '@') === 0) {
                        // if PHP version is 5.5+
                        if (version_compare(PHP_VERSION, '5.5') >= 0) {
                            // remove the @ from the name
                            $file = substr($value, 1);
                            // use CURLFile to prepare the file
                            $value = new CURLFile($file);
                        }
                    }
                });
            }
            // add each URL and associated properties to the "_requests" property
            $this->_requests[] = array('url' => $url, 'options' => array(CURLINFO_HEADER_OUT => 1, CURLOPT_HEADER => 1, CURLOPT_NOBODY => 0, CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $values, CURLOPT_BINARYTRANSFER => null, CURLOPT_CUSTOMREQUEST => null, CURLOPT_HTTPGET => null, CURLOPT_FILE => null), 'callback' => $callback, 'arguments' => array_slice(func_get_args(), 2));
        }
        // if we're just queuing requests for now, do not execute the next lines
        if ($this->_queue) {
            return;
        }
        // if we have to pause between batches of requests, process them sequentially, in batches
        if ($this->pause_interval > 0) {
            $this->_process_paused();
        } else {
            $this->_process();
        }
    }