本文整理汇总了PHP中HttpRequest::setCookies方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpRequest::setCookies方法的具体用法?PHP HttpRequest::setCookies怎么用?PHP HttpRequest::setCookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpRequest
的用法示例。
在下文中一共展示了HttpRequest::setCookies方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: HttpRequest
<?php
$request = new HttpRequest();
$request->setUrl('http://mockbin.com/har');
$request->setMethod(HTTP_METH_POST);
$request->setCookies(array('bar' => 'baz', 'foo' => 'bar'));
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
示例2: header
if (isset($_SERVER['HTTP_REFERER'])) {
$options['referer'] = $conf->deproxify($_SERVER['HTTP_REFERER']);
}
$request->addHeaders($headers);
$request->setOptions($options);
// handle GET / POST requests and data
if (!empty($_POST)) {
$request->setMethod(HttpRequest::METH_POST);
$request->setPostFields($_POST);
} else {
// GET Request
$request->setMethod(HttpRequest::METH_GET);
}
// Cookie handling
$request->enableCookies = true;
$request->setCookies($_COOKIE);
try {
// HttpRequest can follow redirects automatically, but that
// works strangely...
$max_follow_redirects = 10;
do {
$response = $request->send();
if ($response->getResponseCode() >= 400) {
// Error codes. Redirect client to error page so he
// can see the mess on his own ;-)
header("Location: {$url}");
exit;
}
if ($response->getResponseCode() != 301 && $response->getResponseCode() != 302) {
// no redirection codes, go on in program...
break;
示例3: factory
/**
* @return IHttpRequest
*/
public function factory()
{
$useFilter = !in_array(ini_get('filter.default'), ['', 'unsafe_raw']) || ini_get('filter.default_flags');
$post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? [] : $_POST);
$cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? [] : $_COOKIE);
$blacklist = '\\x09\\x0A\\x0D\\x20-\\x7E\\xA0-\\x{10FFFF}';
$chars = '~^[' . $blacklist . ']*+\\z~u';
if (!$this->binary) {
$list = [&$post, &$cookies];
while (list($key, $source) = each($list)) {
foreach ($source as $k => $v) {
if (is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
unset($list[$key][$k]);
} else {
if (is_array($v)) {
$list[$key][$k] = $v;
$list[] =& $list[$key][$k];
} else {
$list[$key][$k] = (string) preg_replace('~[^' . $blacklist . ']+~u', '', $v);
}
}
}
}
unset($list, $key, $source, $k, $v);
}
$uploadList = [];
$list = [];
if (!empty($_FILES)) {
foreach ($_FILES as $k => $v) {
if (!$this->binary && is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
continue;
}
$v['@'] =& $uploadList[$k];
$list[] = $v;
}
}
while (list(, $upload) = each($list)) {
if (!isset($upload['name'])) {
continue;
} else {
if (!is_array($upload['name'])) {
if (!$this->binary && (!preg_match($chars, $upload['name']) || preg_last_error())) {
$upload['name'] = '';
}
if ($upload['error'] !== UPLOAD_ERR_NO_FILE) {
$upload['@'] = new Upload($upload);
}
continue;
}
}
foreach ($upload['name'] as $k => $foo) {
if (!$this->binary && is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
continue;
}
$list[] = ['name' => $upload['name'][$k], 'type' => $upload['type'][$k], 'size' => $upload['size'][$k], 'tmp_name' => $upload['tmp_name'][$k], 'error' => $upload['error'][$k], '@' => &$upload['@'][$k]];
}
}
$remoteAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
$remoteHost = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null;
$method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
if ($method === 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) && preg_match('#^[A-Z]+\\z#', $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
}
$headers = [];
foreach ($_SERVER as $k => $v) {
if (strncmp($k, 'HTTP_', 5) == 0) {
$k = substr($k, 5);
} else {
if (strncmp($k, 'CONTENT_', 8)) {
continue;
}
}
$headers[strtr($k, '_', '-')] = $v;
}
$httpRequest = new HttpRequest();
$httpRequest->setUrl(Url::getCurrent());
$httpRequest->setMethod($method);
$httpRequest->setPost($post);
$httpRequest->setUploadList($uploadList);
$httpRequest->setCookies($cookies);
$httpRequest->setHeaders($headers);
$httpRequest->setRemoteAddress($remoteAddress);
$httpRequest->setRemoteHost($remoteHost);
$httpRequest->setBody(file_get_contents('php://input'));
return $httpRequest;
}
示例4: _http_execute
/**
* Execute the request using the PECL HTTP extension. (recommended)
*
* @param Request $request Request to execute
* @return Response
*/
protected function _http_execute(Request $request)
{
$http_method_mapping = array(Http_Request::GET => HttpRequest::METH_GET, Http_Request::HEAD => HttpRequest::METH_HEAD, Http_Request::POST => HttpRequest::METH_POST, Http_Request::PUT => HttpRequest::METH_PUT, Http_Request::DELETE => HttpRequest::METH_DELETE, Http_Request::OPTIONS => HttpRequest::METH_OPTIONS, Http_Request::TRACE => HttpRequest::METH_TRACE, Http_Request::CONNECT => HttpRequest::METH_CONNECT);
// Create an http request object
$http_request = new HttpRequest($request->uri(), $http_method_mapping[$request->method()]);
// Set custom options
$http_request->setOptions($this->_options);
// Set headers
$http_request->setHeaders($request->headers()->getArrayCopy());
// Set cookies
$http_request->setCookies($request->cookie());
// Set body
$http_request->setBody($request->body());
try {
$http_request->send();
} catch (HttpRequestException $e) {
throw new Kohana_Request_Exception($e->getMessage());
} catch (HttpMalformedHeaderException $e) {
throw new Kohana_Request_Exception($e->getMessage());
} catch (HttpEncodingException $e) {
throw new Kohana_Request_Exception($e->getMessage());
}
// Create the response
$response = $request->create_response();
// Build the response
$response->status($http_request->getResponseCode())->headers($http_request->getResponseHeader())->cookie($http_request->getResponseCookies())->body($http_request->getResponseBody());
return $response;
}