PHP Get Request, Sending Headers

PHP GET Request, sending headers

If you're using cURL, you can use curl_setopt ($handle, CURLOPT_USERAGENT, 'browser description') to define the user-agent header of the request.

If you're using file_get_contents, check out this tweak of an example on the man page for file_get_contents:

// Create a stream
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n" .
"User-agent: BROWSER-DESCRIPTION-HERE\r\n"
)
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents('http://www.example.com/', false, $context);

How do I send a GET request with a header from PHP?

You may use file_get_contents if you don't want to use curl but not sure about speed but it's php's built in function where curl is not. When talking about speed then I think whatever you use for a remote request, the speed/performance will depend on the network connection speed more than the function/library and maybe there is a bit different among these (curl/file_get_contents/fsockopen) but I think it'll be a very little (1-2 %) and you can't catch the difference, it'll seem almost same.

$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"X-Header-Name: $foobar"
));
$context = stream_context_create($opts);
$data = file_get_contents('http://www.example.com/hello.xyz', false, $context);
if($data) {
// do something with data
}

Also, if you want to use curl then you may use this

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Header-Name: $foobar"));
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/hello.xyz");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($curl_errno == 0) {
// $data received in $data
}

Also, check this answer, it may help you to decide.

PHP Using cURL and GET request with a header

According to PHP documentation for CURLOPT_HEADER:

TRUE to include the header in the output.

Your $response will probably look like this:

HTTP/1.1 200 OK
Some: headers
More: header lines

{
"real": "json content"
}

This is because you added the CURLOPT_HEADER option.

You don't need to set any options to let the curl request send your headers. As long as you set the CURLOPT_HTTPHEADER option, the headers will be sent.

If you really want to receive the response headers too, check existing questions like "Can PHP cURL retrieve response headers AND body in a single request?"

How to read the php request header values?

From your screenshot (which appears to be from a browser's Network tool) it looks like you are talking about reading the header values which were received by the PHP script from the request your browser sent to PHP. That's nothing to do with cURL - cURL is for sending HTTP requests from your PHP script to another URL...it's unrelated to the interaction between the client-side and PHP via your webserver.

To read the headers which are incoming into your PHP script from the browser (or other client) making the request, you can use getallheaders() which returns an associative array of all the received headers.

e.g. to simply list them all:

foreach (getallheaders() as $name => $value) {
echo "$name: $value\n";
}

Documentation: https://www.php.net/manual/en/function.getallheaders.php

Send GET HTTPS request with custom headers PHP

You are missing a curl option for handling HTTPS request that is

CURLOPT_SSL_VERIFYPEER : FALSE to stop cURL from verifying the peer's certificate

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

How do I read any request header in PHP

IF: you only need a single header, instead of all headers, the quickest method is:

<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE (and with '-' replaced by '_')
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];


ELSE IF: you run PHP as an Apache module or, as of PHP 5.4, using FastCGI (simple method):

apache_request_headers()

<?php
$headers = apache_request_headers();

foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}


ELSE: In any other case, you can use (userland implementation):

<?php
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}

$headers = getRequestHeaders();

foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}


See Also:

getallheaders() - (PHP >= 5.4) cross platform edition Alias of apache_request_headers()
apache_response_headers() - Fetch all HTTP response headers.

headers_list() - Fetch a list of headers to be sent.



Related Topics



Leave a reply



Submit