本文整理汇总了PHP中HTTPRequest::addHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP HTTPRequest::addHeader方法的具体用法?PHP HTTPRequest::addHeader怎么用?PHP HTTPRequest::addHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HTTPRequest
的用法示例。
在下文中一共展示了HTTPRequest::addHeader方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test
/**
* Test a URL request, returning a response object.
*
* This method is the counterpart of Director::direct() that is used in functional testing. It will execute the URL given,
*
* @param string $url The URL to visit
* @param array $postVars The $_POST & $_FILES variables
* @param Session $session The {@link Session} object representing the current session. By passing the same object to multiple
* calls of Director::test(), you can simulate a peristed session.
* @param string $httpMethod The HTTP method, such as GET or POST. It will default to POST if postVars is set, GET otherwise.
* Overwritten by $postVars['_method'] if present.
* @param string $body The HTTP body
* @param array $headers HTTP headers with key-value pairs
* @return HTTPResponse
*
* @uses getControllerForURL() The rule-lookup logic is handled by this.
* @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
*/
static function test($url, $postVars = null, $session = null, $httpMethod = null, $body = null, $headers = null)
{
// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
// Really, it's some inapproriate coupling and should be resolved by making less use of statics
$oldStage = Versioned::current_stage();
$getVars = array();
if (!$httpMethod) {
$httpMethod = $postVars || is_array($postVars) ? "POST" : "GET";
}
$urlWithQuerystring = $url;
if (strpos($url, '?') !== false) {
list($url, $getVarsEncoded) = explode('?', $url, 2);
parse_str($getVarsEncoded, $getVars);
}
if (!$session) {
$session = new Session(null);
}
// Back up the current values of the superglobals
$existingRequestVars = $_REQUEST;
$existingGetVars = $_GET;
$existingPostVars = $_POST;
$existingSessionVars = $_SESSION;
$existingCookies = $_COOKIE;
$existingServer = $_SERVER;
$existingCookieReportErrors = Cookie::report_errors();
$existingRequirementsBackend = Requirements::backend();
Cookie::set_report_errors(false);
Requirements::set_backend(new Requirements_Backend());
// Replace the superglobals with appropriate test values
$_REQUEST = array_merge((array) $getVars, (array) $postVars);
$_GET = (array) $getVars;
$_POST = (array) $postVars;
$_SESSION = $session ? $session->inst_getAll() : array();
$_COOKIE = array();
$_SERVER['REQUEST_URI'] = Director::baseURL() . $urlWithQuerystring;
$req = new HTTPRequest($httpMethod, $url, $getVars, $postVars, $body);
if ($headers) {
foreach ($headers as $k => $v) {
$req->addHeader($k, $v);
}
}
$result = Director::handleRequest($req, $session);
// Restore the superglobals
$_REQUEST = $existingRequestVars;
$_GET = $existingGetVars;
$_POST = $existingPostVars;
$_SESSION = $existingSessionVars;
$_COOKIE = $existingCookies;
$_SERVER = $existingServer;
Cookie::set_report_errors($existingCookieReportErrors);
Requirements::set_backend($existingRequirementsBackend);
// These are needed so that calling Director::test() doesnt muck with whoever is calling it.
// Really, it's some inapproriate coupling and should be resolved by making less use of statics
Versioned::reading_stage($oldStage);
return $result;
}