本文整理汇总了PHP中HttpRequest::getRequestMethod方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpRequest::getRequestMethod方法的具体用法?PHP HttpRequest::getRequestMethod怎么用?PHP HttpRequest::getRequestMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpRequest
的用法示例。
在下文中一共展示了HttpRequest::getRequestMethod方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: makeRequest
public static function makeRequest(HttpRequest $request)
{
$requestUri = $request->getRequestUri()->getUri();
// if the request is towards a file URL, return the response constructed
// from file
if (0 === strpos($requestUri, "file:///")) {
return HttpResponse::fromFile($requestUri);
}
$httpResponse = new HttpResponse();
$curlChannel = curl_init();
curl_setopt($curlChannel, CURLOPT_URL, $requestUri);
curl_setopt($curlChannel, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curlChannel, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlChannel, CURLOPT_TIMEOUT, 10);
if ($request->getRequestMethod() === "POST") {
curl_setopt($curlChannel, CURLOPT_POST, 1);
curl_setopt($curlChannel, CURLOPT_POSTFIELDS, $request->getContent());
}
$basicAuthUser = $request->getBasicAuthUser();
$basicAuthPass = $request->getBasicAuthPass();
if (NULL !== $basicAuthUser) {
$request->setHeader("Authorization", "Basic " . base64_encode($basicAuthUser . ":" . $basicAuthPass));
}
// Including HTTP headers in request
$headers = $request->getHeaders(TRUE);
if (!empty($headers)) {
curl_setopt($curlChannel, CURLOPT_HTTPHEADER, $headers);
}
// Connect to SSL/TLS server, validate certificate and host
if ($request->getRequestUri()->getScheme() === "https") {
curl_setopt($curlChannel, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curlChannel, CURLOPT_SSL_VERIFYHOST, 2);
}
// Callback to extract all the HTTP headers from the response...
// In order to really correctly parse HTTP headers one would have to look at RFC 2616...
curl_setopt($curlChannel, CURLOPT_HEADERFUNCTION, function ($curlChannel, $header) use($httpResponse) {
// Ignore Status-Line (RFC 2616, section 6.1)
if (0 === preg_match('|^HTTP/\\d+.\\d+ [1-5]\\d\\d|', $header)) {
// Only deal with header lines that contain a colon
if (strpos($header, ":") !== FALSE) {
// Only deal with header lines that contain a colon
list($key, $value) = explode(":", trim($header));
$httpResponse->setHeader(trim($key), trim($value));
}
}
return strlen($header);
});
$output = curl_exec($curlChannel);
if ($errorNumber = curl_errno($curlChannel)) {
throw new OutgoingHttpRequestException(curl_error($curlChannel));
}
$httpResponse->setStatusCode(curl_getinfo($curlChannel, CURLINFO_HTTP_CODE));
$httpResponse->setContent($output);
curl_close($curlChannel);
return $httpResponse;
}
示例2: isRequestCacheable
/**
* Check if an HTTP request can be cached by a private local cache.
*
* @static
* @param HttpRequest $resp
* @return bool True if the request is cacheable.
* False if the request is uncacheable.
*/
public static function isRequestCacheable(HttpRequest $resp)
{
$method = $resp->getRequestMethod();
if (!in_array($method, self::$CACHEABLE_HTTP_METHODS)) {
return false;
}
// Don't cache authorized requests/responses.
// [rfc2616-14.8] When a shared cache receives a request containing an
// Authorization field, it MUST NOT return the corresponding response
// as a reply to any other request...
if ($resp->getRequestHeader("authorization")) {
return false;
}
return true;
}
示例3: sendRequest
public function sendRequest(HttpRequest $request)
{
if (!$this->client) {
throw new Exception\TwitchException('Client was not set');
}
$apiurl = rtrim($this->client->getApiUrl(), '/');
$url = sprintf('%s/%s', $apiurl, $request->getUrl());
$url = rtrim($url, '/');
$this->getAdapter()->open();
$data = $this->getAdapter()->send($url, $request->getRequestMethod(), $request->getRequestHeaders(), $request->getPostBody());
$status_code = $this->getAdapter()->getHttpStatusCode();
$data = json_decode($data);
$this->getAdapter()->close();
// TODO we need to return more info
if ($status_code !== 200) {
throw new Exception\TwitchException($data->error, $data->status);
}
return $data;
}