本文整理汇总了PHP中WP_Http::buildCookieHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP WP_Http::buildCookieHeader方法的具体用法?PHP WP_Http::buildCookieHeader怎么用?PHP WP_Http::buildCookieHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WP_Http
的用法示例。
在下文中一共展示了WP_Http::buildCookieHeader方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: request
/**
* Send a HTTP request to a URI using cURL extension.
*
* @access public
* @since 2.7.0
*
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
*/
public function request($url, $args = array())
{
$defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array());
$r = wp_parse_args($args, $defaults);
if (isset($r['headers']['User-Agent'])) {
$r['user-agent'] = $r['headers']['User-Agent'];
unset($r['headers']['User-Agent']);
} elseif (isset($r['headers']['user-agent'])) {
$r['user-agent'] = $r['headers']['user-agent'];
unset($r['headers']['user-agent']);
}
// Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader($r);
$handle = curl_init();
// cURL offers really easy proxy support.
$proxy = new WP_HTTP_Proxy();
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($handle, CURLOPT_PROXY, $proxy->host());
curl_setopt($handle, CURLOPT_PROXYPORT, $proxy->port());
if ($proxy->use_authentication()) {
curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
curl_setopt($handle, CURLOPT_PROXYUSERPWD, $proxy->authentication());
}
}
$is_local = isset($r['local']) && $r['local'];
$ssl_verify = isset($r['sslverify']) && $r['sslverify'];
if ($is_local) {
/** This filter is documented in wp-includes/class-http.php */
$ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
} elseif (!$is_local) {
/** This filter is documented in wp-includes/class-http.php */
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
}
/*
* CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since.
* a value of 0 will allow an unlimited timeout.
*/
$timeout = (int) ceil($r['timeout']);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_TIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify === true ? 2 : false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
curl_setopt($handle, CURLOPT_CAINFO, $r['sslcertificates']);
curl_setopt($handle, CURLOPT_USERAGENT, $r['user-agent']);
/*
* The option doesn't work with safe mode or when open_basedir is set, and there's
* a bug #17490 with redirected POST requests, so handle redirections outside Curl.
*/
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, false);
if (defined('CURLOPT_PROTOCOLS')) {
// PHP 5.2.10 / cURL 7.19.4
curl_setopt($handle, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
}
switch ($r['method']) {
case 'HEAD':
curl_setopt($handle, CURLOPT_NOBODY, true);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
default:
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, $r['method']);
if (!is_null($r['body'])) {
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
}
break;
}
if (true === $r['blocking']) {
curl_setopt($handle, CURLOPT_HEADERFUNCTION, array($this, 'stream_headers'));
curl_setopt($handle, CURLOPT_WRITEFUNCTION, array($this, 'stream_body'));
}
curl_setopt($handle, CURLOPT_HEADER, false);
if (isset($r['limit_response_size'])) {
$this->max_body_length = intval($r['limit_response_size']);
} else {
$this->max_body_length = false;
}
// If streaming to a file open a file handle, and setup our curl streaming handler.
if ($r['stream']) {
if (!WP_DEBUG) {
$this->stream_handle = @fopen($r['filename'], 'w+');
} else {
//.........这里部分代码省略.........
示例2: request
/**
* Send a HTTP request to a URI using PHP Streams.
*
* @see WP_Http::request For default options descriptions.
*
* @since 2.7.0
* @since 3.7.0 Combined with the fsockopen transport and switched to stream_socket_client().
*
* @access public
* @param string $url The request URL.
* @param string|array $args Optional. Override the defaults.
* @return array|WP_Error Array containing 'headers', 'body', 'response', 'cookies', 'filename'. A WP_Error instance upon error
*/
public function request($url, $args = array())
{
$defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array());
$r = wp_parse_args($args, $defaults);
if (isset($r['headers']['User-Agent'])) {
$r['user-agent'] = $r['headers']['User-Agent'];
unset($r['headers']['User-Agent']);
} elseif (isset($r['headers']['user-agent'])) {
$r['user-agent'] = $r['headers']['user-agent'];
unset($r['headers']['user-agent']);
}
// Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader($r);
$arrURL = parse_url($url);
$connect_host = $arrURL['host'];
$secure_transport = $arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https';
if (!isset($arrURL['port'])) {
if ($arrURL['scheme'] == 'ssl' || $arrURL['scheme'] == 'https') {
$arrURL['port'] = 443;
$secure_transport = true;
} else {
$arrURL['port'] = 80;
}
}
// Always pass a Path, defaulting to the root in cases such as http://example.com
if (!isset($arrURL['path'])) {
$arrURL['path'] = '/';
}
if (isset($r['headers']['Host']) || isset($r['headers']['host'])) {
if (isset($r['headers']['Host'])) {
$arrURL['host'] = $r['headers']['Host'];
} else {
$arrURL['host'] = $r['headers']['host'];
}
unset($r['headers']['Host'], $r['headers']['host']);
}
/*
* Certain versions of PHP have issues with 'localhost' and IPv6, It attempts to connect
* to ::1, which fails when the server is not set up for it. For compatibility, always
* connect to the IPv4 address.
*/
if ('localhost' == strtolower($connect_host)) {
$connect_host = '127.0.0.1';
}
$connect_host = $secure_transport ? 'ssl://' . $connect_host : 'tcp://' . $connect_host;
$is_local = isset($r['local']) && $r['local'];
$ssl_verify = isset($r['sslverify']) && $r['sslverify'];
if ($is_local) {
/**
* Filter whether SSL should be verified for local requests.
*
* @since 2.8.0
*
* @param bool $ssl_verify Whether to verify the SSL connection. Default true.
*/
$ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
} elseif (!$is_local) {
/**
* Filter whether SSL should be verified for non-local requests.
*
* @since 2.8.0
*
* @param bool $ssl_verify Whether to verify the SSL connection. Default true.
*/
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
}
$proxy = new WP_HTTP_Proxy();
$context = stream_context_create(array('ssl' => array('verify_peer' => $ssl_verify, 'capture_peer_cert' => $ssl_verify, 'SNI_enabled' => true, 'cafile' => $r['sslcertificates'], 'allow_self_signed' => !$ssl_verify)));
$timeout = (int) floor($r['timeout']);
$utimeout = $timeout == $r['timeout'] ? 0 : 1000000 * $r['timeout'] % 1000000;
$connect_timeout = max($timeout, 1);
// Store error number.
$connection_error = null;
// Store error string.
$connection_error_str = null;
if (!WP_DEBUG) {
// In the event that the SSL connection fails, silence the many PHP Warnings.
if ($secure_transport) {
$error_reporting = error_reporting(0);
}
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
$handle = @stream_socket_client('tcp://' . $proxy->host() . ':' . $proxy->port(), $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context);
} else {
$handle = @stream_socket_client($connect_host . ':' . $arrURL['port'], $connection_error, $connection_error_str, $connect_timeout, STREAM_CLIENT_CONNECT, $context);
}
if ($secure_transport) {
error_reporting($error_reporting);
//.........这里部分代码省略.........
示例3: request
/**
* Send a HTTP request to a URI using cURL extension.
*
* @access public
* @since 2.7.0
*
* @param string $url
* @param str|array $args Optional. Override the defaults.
* @return array 'headers', 'body', 'response', 'cookies' and 'filename' keys.
*/
function request($url, $args = array())
{
$defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array());
$r = wp_parse_args($args, $defaults);
if (isset($r['headers']['User-Agent'])) {
$r['user-agent'] = $r['headers']['User-Agent'];
unset($r['headers']['User-Agent']);
} else {
if (isset($r['headers']['user-agent'])) {
$r['user-agent'] = $r['headers']['user-agent'];
unset($r['headers']['user-agent']);
}
}
// Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader($r);
$handle = curl_init();
// cURL offers really easy proxy support.
$proxy = new WP_HTTP_Proxy();
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($handle, CURLOPT_PROXY, $proxy->host());
curl_setopt($handle, CURLOPT_PROXYPORT, $proxy->port());
if ($proxy->use_authentication()) {
curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
curl_setopt($handle, CURLOPT_PROXYUSERPWD, $proxy->authentication());
}
}
$is_local = isset($args['local']) && $args['local'];
$ssl_verify = isset($args['sslverify']) && $args['sslverify'];
if ($is_local) {
$ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
} elseif (!$is_local) {
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
}
// CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT expect integers. Have to use ceil since
// a value of 0 will allow an ulimited timeout.
$timeout = (int) ceil($r['timeout']);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_TIMEOUT, $timeout);
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify === true ? 2 : false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
curl_setopt($handle, CURLOPT_USERAGENT, $r['user-agent']);
curl_setopt($handle, CURLOPT_MAXREDIRS, $r['redirection']);
switch ($r['method']) {
case 'HEAD':
curl_setopt($handle, CURLOPT_NOBODY, true);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
}
if (true === $r['blocking']) {
curl_setopt($handle, CURLOPT_HEADERFUNCTION, array(&$this, 'stream_headers'));
}
curl_setopt($handle, CURLOPT_HEADER, false);
// If streaming to a file open a file handle, and setup our curl streaming handler
if ($r['stream']) {
if (!WP_DEBUG) {
$stream_handle = @fopen($r['filename'], 'w+');
} else {
$stream_handle = fopen($r['filename'], 'w+');
}
if (!$stream_handle) {
return new WP_Error('http_request_failed', sprintf(__('Could not open handle for fopen() to %s'), $r['filename']));
}
curl_setopt($handle, CURLOPT_FILE, $stream_handle);
}
// The option doesn't work with safe mode or when open_basedir is set.
if (!ini_get('safe_mode') && !ini_get('open_basedir') && 0 !== $r['_redirection']) {
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
}
if (!empty($r['headers'])) {
// cURL expects full header strings in each element
$headers = array();
foreach ($r['headers'] as $name => $value) {
$headers[] = "{$name}: {$value}";
}
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
}
if ($r['httpversion'] == '1.0') {
curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
} else {
curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
//.........这里部分代码省略.........
示例4: request
/**
* Send a HTTP request to a URI using cURL extension.
*
* @access public
* @since 2.7.0
*
* @param string $url
* @param str|array $args Optional. Override the defaults.
* @return array 'headers', 'body', 'cookies' and 'response' keys.
*/
function request($url, $args = array())
{
$defaults = array('method' => 'GET', 'timeout' => 5, 'redirection' => 5, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => null, 'cookies' => array());
$r = wp_parse_args($args, $defaults);
if (isset($r['headers']['User-Agent'])) {
$r['user-agent'] = $r['headers']['User-Agent'];
unset($r['headers']['User-Agent']);
} else {
if (isset($r['headers']['user-agent'])) {
$r['user-agent'] = $r['headers']['user-agent'];
unset($r['headers']['user-agent']);
}
}
// Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader($r);
// cURL extension will sometimes fail when the timeout is less than 1 as it may round down
// to 0, which gives it unlimited timeout.
if ($r['timeout'] > 0 && $r['timeout'] < 1) {
$r['timeout'] = 1;
}
$handle = curl_init();
// cURL offers really easy proxy support.
$proxy = new WP_HTTP_Proxy();
if ($proxy->is_enabled() && $proxy->send_through_proxy($url)) {
$isPHP5 = version_compare(PHP_VERSION, '5.0.0', '>=');
if ($isPHP5) {
curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($handle, CURLOPT_PROXY, $proxy->host());
curl_setopt($handle, CURLOPT_PROXYPORT, $proxy->port());
} else {
curl_setopt($handle, CURLOPT_PROXY, $proxy->host() . ':' . $proxy->port());
}
if ($proxy->use_authentication()) {
if ($isPHP5) {
curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
}
curl_setopt($handle, CURLOPT_PROXYUSERPWD, $proxy->authentication());
}
}
$is_local = isset($args['local']) && $args['local'];
$ssl_verify = isset($args['sslverify']) && $args['sslverify'];
if ($is_local) {
$ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
} elseif (!$is_local) {
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
}
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, $ssl_verify);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
curl_setopt($handle, CURLOPT_USERAGENT, $r['user-agent']);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, $r['timeout']);
curl_setopt($handle, CURLOPT_TIMEOUT, $r['timeout']);
curl_setopt($handle, CURLOPT_MAXREDIRS, $r['redirection']);
switch ($r['method']) {
case 'HEAD':
curl_setopt($handle, CURLOPT_NOBODY, true);
break;
case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $r['body']);
break;
}
if (true === $r['blocking']) {
curl_setopt($handle, CURLOPT_HEADER, true);
} else {
curl_setopt($handle, CURLOPT_HEADER, false);
}
// The option doesn't work with safe mode or when open_basedir is set.
if (!ini_get('safe_mode') && !ini_get('open_basedir')) {
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);
}
if (!empty($r['headers'])) {
// cURL expects full header strings in each element
$headers = array();
foreach ($r['headers'] as $name => $value) {
$headers[] = "{$name}: {$value}";
}
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
}
if ($r['httpversion'] == '1.0') {
curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
} else {
curl_setopt($handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
}
// Cookies are not handled by the HTTP API currently. Allow for plugin authors to handle it
//.........这里部分代码省略.........
示例5: request
/**
* Make a HTTP request to a supplied URL.
*
* Much of the logic here is plagiarized from the WP_Http_Curl as to avoid
* trying to re-invent the wheel.
*
* @param $url
* @param $args
* @return bool
*/
public function request($url, $args)
{
$r = wp_parse_args($args, self::$request_defaults);
// Construct Cookie: header if any cookies are set.
WP_Http::buildCookieHeader($r);
$is_local = isset($r['local']) && $r['local'];
$ssl_verify = isset($r['sslverify']) && $r['sslverify'];
if ($is_local) {
$ssl_verify = apply_filters('https_local_ssl_verify', $ssl_verify);
} else {
$ssl_verify = apply_filters('https_ssl_verify', $ssl_verify);
}
// For now, lets not support streaming into a file and see what breaks
if (isset($r['filename'])) {
return new WP_Error('http_request_failed', __('Saving to a file is not currently supported.'));
}
$req = new \google\appengine\URLFetchRequest();
$req->setUrl($url);
$req->setMethod(self::$request_map[$r['method']]);
$req->setMustValidateServerCertificate($ssl_verify);
if (isset($r['body'])) {
$req->setPayload($r['body']);
}
if (isset($r['timeout'])) {
$req->setDeadline($r['timeout']);
}
// App Engine does not allow setting the number of redirects, only if we
// follow redirects or not.
$req->setFollowRedirects(isset($r['redirection']) && $r['redirection'] != 0);
foreach ($r['headers'] as $key => $value) {
$header = $req->addHeader();
$header->setKey($key);
$header->setValue($value);
}
$resp = new \google\appengine\URLFetchResponse();
try {
ApiProxy::makeSyncCall('urlfetch', 'Fetch', $req, $resp);
} catch (ApplicationError $e) {
syslog(LOG_ERR, sprintf("Call to URLFetch failed with application error %d for url %s.", $e->getApplicationError(), $url));
return new \WP_Error('http_request_failed', $e->getMessage());
}
$response = [];
$response['code'] = $resp->getStatusCode();
$response['message'] = \get_status_header_desc($resp->getStatusCode());
$headers = [];
$cookies = [];
foreach ($resp->getHeaderList() as $header) {
$key = strtolower($header->getKey());
$value = trim($header->getValue());
// If a header has multiple values then it is stored in an array
if (isset($headers[$key])) {
if (!is_array($headers[$key])) {
$headers[$key] = [$headers[$key]];
}
$headers[$key][] = $value;
} else {
$headers[$key] = $value;
}
if ('set-cookie' == $key) {
$cookies[] = new \WP_Http_Cookie($value, $url);
}
}
$theBody = $resp->getContent();
if (true === $r['decompress'] && true === WP_Http_Encoding::should_decode($headers)) {
$theBody = WP_Http_Encoding::decompress($theBody);
}
if (isset($r['limit_response_size']) && strlen($theBody) > $r['limit_response_size']) {
$theBody = substr($theBody, 0, $r['limit_response_size']);
}
$response = ['response' => $response, 'body' => $theBody, 'cookies' => $cookies, 'headers' => $headers];
return $response;
}