/** * Purge content * * @param string $path * @param int $type * @param string $error * @return boolean */ function _purge_content($path, $type, &$error) { $url = sprintf(W3TC_CDN_EDGECAST_PURGE_URL, $this->_config['account']); $args = array('method' => 'PUT', 'user-agent' => W3TC_POWERED_BY, 'headers' => array('Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => sprintf('TOK:%s', $this->_config['token'])), 'body' => json_encode(array('MediaPath' => $path, 'MediaType' => $type))); $response = nxt_remote_request($url, $args); if (is_nxt_error($response)) { $error = implode('; ', $response->get_error_messages()); return false; } switch ($response['response']['code']) { case 200: return true; case 400: $error = 'Invalid Request Parameter'; return false; case 403: $error = 'Authentication Failure or Insufficient Access Rights'; return false; case 404: $error = 'Invalid Request URI'; return false; case 405: $error = 'Invalid Request'; return false; case 500: $error = 'Server Error'; return false; } $error = 'Unknown error'; return false; }
function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false) { $this->url = $url; $this->timeout = $timeout; $this->redirects = $redirects; $this->headers = $headers; $this->useragent = $useragent; $this->method = SIMPLEPIE_FILE_SOURCE_REMOTE; if (preg_match('/^http(s)?:\\/\\//i', $url)) { $args = array('timeout' => $this->timeout, 'redirection' => $this->redirects); if (!empty($this->headers)) { $args['headers'] = $this->headers; } if (SIMPLEPIE_USERAGENT != $this->useragent) { //Use default nxt user agent unless custom has been specified $args['user-agent'] = $this->useragent; } $res = nxt_remote_request($url, $args); if (is_nxt_error($res)) { $this->error = 'nxt HTTP Error: ' . $res->get_error_message(); $this->success = false; } else { $this->headers = nxt_remote_retrieve_headers($res); $this->body = nxt_remote_retrieve_body($res); $this->status_code = nxt_remote_retrieve_response_code($res); } } else { if (!($this->body = file_get_contents($url))) { $this->error = 'file_get_contents could not read the file'; $this->success = false; } } }
/** * Sends HTTP request * * @param $url string * @param $args array * @return nxt_Error|array */ function w3_http_request($url, $args = array()) { $args = array_merge(array('user-agent' => W3TC_POWERED_BY), $args); return nxt_remote_request($url, $args); }
/** * Perform a HTTP HEAD or GET request. * * If $file_path is a writable filename, this will do a GET request and write * the file to that path. * * @since 2.5.0 * * @param string $url URL to fetch. * @param string|bool $file_path Optional. File path to write request to. * @param int $red (private) The number of Redirects followed, Upon 5 being hit, returns false. * @return bool|string False on failure and string of headers if HEAD request. */ function nxt_get_http($url, $file_path = false, $red = 1) { @set_time_limit(60); if ($red > 5) { return false; } $options = array(); $options['redirection'] = 5; if (false == $file_path) { $options['method'] = 'HEAD'; } else { $options['method'] = 'GET'; } $response = nxt_remote_request($url, $options); if (is_nxt_error($response)) { return false; } $headers = nxt_remote_retrieve_headers($response); $headers['response'] = nxt_remote_retrieve_response_code($response); // nxt_HTTP no longer follows redirects for HEAD requests. if ('HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset($headers['location'])) { return nxt_get_http($headers['location'], $file_path, ++$red); } if (false == $file_path) { return $headers; } // GET request - write it to the supplied filename $out_fp = fopen($file_path, 'w'); if (!$out_fp) { return $headers; } fwrite($out_fp, nxt_remote_retrieve_body($response)); fclose($out_fp); clearstatcache(); return $headers; }
/** * GET URL * * @param string $url * @param string $username * @param string $password * @param bool $head * @return array */ function get_page($url, $username = '', $password = '', $head = false) { // Increase the timeout add_filter('http_request_timeout', array(&$this, 'bump_request_timeout')); $headers = array(); $args = array(); if (true === $head) { $args['method'] = 'HEAD'; } if (!empty($username) && !empty($password)) { $headers['Authorization'] = 'Basic ' . base64_encode("{$username}:{$password}"); } $args['headers'] = $headers; return nxt_remote_request($url, $args); }