Example #1
0
/**
 * Wrapper to provide a single interface for making an HTTP request.
 *
 * Attempts to use cURL, fopen(), or fsockopen(), whichever is available
 * first.
 *
 * @param	string	$url		URL to make request to.
 * @param	array	$postdata	(optional) If postdata is provided, the request
 *								method is POST with the key/value pairs as
 *								the data.
 * @param	array	$file		(optional) Should provide associative array
 *								with two keys: name and field.  Name should
 *								be the name of the file and field is the name
 *								of the field to POST.
 */
function dsq_urlopen($url, $postdata = false, $file = false)
{
    $response = array('data' => '', 'code' => 0);
    if ($file) {
        extract($file, EXTR_PREFIX_ALL, 'file');
    }
    if (empty($file_name) || empty($file_field)) {
        $file_name = false;
        $file_field = false;
    }
    //
    // Try curl, fsockopen, fopen + stream (PHP5 only), exec wget
    if (function_exists('curl_init')) {
        if (!function_exists('curl_setopt_array')) {
            function curl_setopt_array(&$ch, $curl_options)
            {
                foreach ($curl_options as $option => $value) {
                    if (!curl_setopt($ch, $option, $value)) {
                        return false;
                    }
                }
                return true;
            }
        }
        _dsq_curl_urlopen($url, $postdata, $response, $file_name, $file_field);
    } else {
        if (ini_get('allow_url_fopen') && function_exists('stream_get_contents')) {
            _dsq_fopen_urlopen($url, $postdata, $response, $file_name, $file_field);
        } else {
            // TODO: Find the failure condition for fsockopen() (sockets?)
            _dsq_fsockopen_urlopen($url, $postdata, $response, $file_name, $file_field);
        }
    }
    // returns array with keys data and code (from headers)
    return $response;
}
Example #2
0
/**
 * Wrapper to provide a single interface for making an HTTP request.
 *
 * Attempts to use cURL, fopen(), or fsockopen(), whichever is available
 * first.
 *
 * @param	string	$url		URL to make request to.
 * @param	array	$postdata	(optional) If postdata is provided, the request
 *								method is POST with the key/value pairs as
 *								the data.
 * @param	array	$file		(optional) Should provide associative array
 *								with two keys: name and field.  Name should
 *								be the name of the file and field is the name
 *								of the field to POST.
 */
function dsq_urlopen($url, $postdata = false, $file = false)
{
    $response = array('data' => '', 'code' => 0);
    if ($file) {
        extract($file, EXTR_PREFIX_ALL, 'file');
    }
    if (empty($file_name) || empty($file_field)) {
        $file_name = false;
        $file_field = false;
    }
    //
    // Try curl, fsockopen, fopen + stream (PHP5 only), exec wget
    // Don't use cURL on IIS servers because it doesn't explicitly specify a CA bundle by default
    if (function_exists('curl_init') && strpos($_SERVER['SERVER_SOFTWARE'], 'IIS') == false) {
        if (!function_exists('curl_setopt_array')) {
            function curl_setopt_array(&$ch, $curl_options)
            {
                foreach ($curl_options as $option => $value) {
                    if (!curl_setopt($ch, $option, $value)) {
                        return false;
                    }
                }
                return true;
            }
        }
        _dsq_curl_urlopen($url, $postdata, $response, $file_name, $file_field);
    } else {
        if (ini_get('allow_url_fopen') && function_exists('stream_get_contents')) {
            _dsq_fopen_urlopen($url, $postdata, $response, $file_name, $file_field);
        } else {
            // TODO: Find the failure condition for fsockopen() (sockets?)
            _dsq_fsockopen_urlopen($url, $postdata, $response, $file_name, $file_field);
        }
    }
    // returns array with keys data and code (from headers)
    return $response;
}