Getting the request headers send via file_get_contents()

Asked
Active3 hr before
Viewed126 times

7 Answers

headersrequestgetting
90%

34 Maybe not, but it answers the opposite question implied in the title, which is how to read the response headers from file_get_contents. And this is where Google lands when investigating THAT question. – Rich Remer Jul 9 '14 at 20:59 ,Unfortunately, it doesn't look like file_get_contents() really offers that degree of control. The cURL extension is usually the first to come up, but I would highly recommend the PECL_HTTP extension (http://pecl.php.net/package/pecl_http) for very simple and straightforward HTTP requests. (it's much easier to work with than cURL), 1 NOTE: Never use single quotes with strings containing special characters like \n or \r. PHP will not interpret them correctly and in case of sending headers, your headers will not get sent correctly. – ak93 Mar 31 '18 at 20:54 ,You can use this variable to retrieve response headers after file_get_contents() function.

Actually, upon further reading on the file_get_contents() function:

// Create a stream
$opts = [
   "http" => [
      "method" => "GET",
      "header" => "Accept-language: en\r\n".
      "Cookie: foo=bar\r\n"
   ]
];

// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
// DOCS: https://www.php.net/manual/en/function.file-get-contents.php
$file = file_get_contents('http://www.example.com/', false, $context);
load more v
88%

$http_response_header — HTTP response headers, The $http_response_header array is similar to the get_headers() function. When using the HTTP wrapper, $http_response_header will be populated with the HTTP response headers. $http_response_header will be created in the local scope. ,Example #1 $http_response_header example, $http_​response_​header

array(9) {
   [0] =>
   string(15)
   "HTTP/1.1 200 OK" [1] =>
   string(35)
   "Date: Sat, 12 Apr 2008 17:30:38 GMT" [2] =>
   string(29)
   "Server: Apache/2.2.3 (CentOS)" [3] =>
   string(44)
   "Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT" [4] =>
   string(27)
   "ETag: "
   280100 - 1 b6 - 80 bfd280 "" [5] =>
      string(20)
   "Accept-Ranges: bytes" [6] =>
   string(19)
   "Content-Length: 438" [7] =>
   string(17)
   "Connection: close" [8] =>
   string(38)
   "Content-Type: text/html; charset=UTF-8"
}
NULL
72%

Search Answer Titles

$opts = [
   "http" => [
      "method" => "GET",
      "header" => "Accept-language: en\r\n".
      "Cookie: foo=bar\r\n"
   ]
];

// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
// DOCS: https://www.php.net/manual/en/function.file-get-contents.php
$file = file_get_contents('http://www.example.com/', false, $context);
load more v
65%

Hello, the function is stream_context_create not stream_create_context. Verry nice solution. I had an environment that doesn't allow curl and this solution work like a charm.,Did you know that using stream contexts, you can set headers when making HTTP requests with php's file_get_contents() function? , We're a place where coders share, stay up-to-date and grow their careers. , We're a place where coders share, stay up-to-date and grow their careers.

$context = stream_create_context([
   "http" => [
      "method" => "GET",
      "header" => "Accept-languange: en\r\n".
      "Cookie: foo=bar\r\n"
   ]
]);

$file = file_get_contents('https://example.com', false, $context);
75%

Actually, upon further reading on the file_get_contents() function:,You can use this variable to retrieve response headers after file_get_contents() function.,You may be able to follow this pattern to achieve what you are seeking to, I haven't personally tested this though. (and if it doesn't work, feel free to check out my other answer)

Actually, upon further reading on the file_get_contents() function:

// Create a stream
$opts = [
   "http" => [
      "method" => "GET",
      "header" => "Accept-language: en\r\n".
      "Cookie: foo=bar\r\n"
   ]
];

// DOCS: https://www.php.net/manual/en/function.stream-context-create.php
$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
// DOCS: https://www.php.net/manual/en/function.file-get-contents.php
$file = file_get_contents('http://www.example.com/', false, $context);
load more v
40%

The request exposes routing parameters through the getParam() method:,Query string parameters can be read using the getQuery() method:,To get all routing parameters as an array use getAttribute():,If you want to access all the query parameters you can use getQueryParams():

$controllerName = $this - > request - > getParam('controller');

// Prior to 3.4.0
$controllerName = $this - > request - > param('controller');
load more v
22%

Use the CURL-less Method Using the stream_context_create() and file_get_contents() Functions to Send a POST Request in PHP,We will introduce a method to send a POST request in PHP using the CURL-less method with the use of the http_build_query(), stream_context_create() and file_get_contents() functions.,Create a User-Defined Function That Uses the CURL to Send the POST Request in PHP,Use the CURL to Send the POST Request in PHP

Example Code:

# php 7. *
   <
   ? php
$postdata = http_build_query(
   array(
      'name' => 'Robert',
      'id' => '1'
   )
);
$opts = array('http' =>
   array(
      'method' => 'POST',
      'header' => 'Content-type: application/x-www-form-urlencoded',
      'content' => $postdata
   )
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost/request.php', false, $context);
echo $result; ?
>
load more v

Other "headers-request" queries related to "Getting the request headers send via file_get_contents()"