Freeman
Freeman

Reputation: 12728

How to send apache headers via php like this method ?

I've an app that generates several apache raw file like below how can I use all of that as request to php ? as I know we can call them separately like header("Location: http://www.example.com/"); but how we can call all of them from one command like this ? (it's just for example)

<?php
        header("http://example.com/test.php

            POST test.php HTTP/1.1
            Host: example.com
            User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0
            Accept: application/json, text/javascript, */*; q=0.01
            Accept-Language: en-US,en;q=0.5
            Accept-Encoding: gzip, deflate, br
            Content-Type: application/x-www-form-urlencoded; charset=UTF-8
            X-Requested-With: XMLHttpRequest
            Referer: http://example.com/test/
            Content-Length: 87
            Cookie: __cfduid=somevalue; 
            Connection: keep-alive
            a=1&b=2");
?>

I want to send it completely to my server is it possible ?

Upvotes: 0

Views: 105

Answers (3)

Alex Blex
Alex Blex

Reputation: 37048

PHP header function sets response header which apache returns to clients. Headers in the question are request headers sent to apache by clients.

From the HTTP/1.1 specification:

6.2 Response Header Fields

The response-header fields allow the server to pass additional information about the response which cannot be placed in the Status- Line. These header fields give information about the server and about further access to the resource identified by the Request-URI.

   response-header = Accept-Ranges           ; Section 14.5
                   | Age                     ; Section 14.6
                   | ETag                    ; Section 14.19
                   | Location                ; Section 14.30
                   | Proxy-Authenticate      ; Section 14.33
                   | Retry-After             ; Section 14.37
                   | Server                  ; Section 14.38
                   | Vary                    ; Section 14.44
                   | WWW-Authenticate        ; Section 14.47

Response-header field names can be extended reliably only in combination with a change in the protocol version. However, new or experimental header fields MAY be given the semantics of response- header fields if all parties in the communication recognize them to be response-header fields. Unrecognized header fields are treated as entity-header fields.

Other RFCs extend this list, but you cannot set referrer, or make browser to submit another POST request via headers.

Upvotes: 1

RichardBernards
RichardBernards

Reputation: 3097

You could extract it from a text and use the header() method after all... Sort of like this:

<?php
$txt = "http://example.com/test.php
POST test.php HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:48.0) Gecko/20100101 Firefox/48.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://example.com/test/
Content-Length: 87
Cookie: __cfduid=somevalue;
Connection: keep-alive
a=1&b=2";

array_map("header", explode("\n", $txt));

So essentially, every new line creates an entry in an array. array_map() traverses the array and for every item in the array, the header() function is invoked with the item in the array as sole argument.

Upvotes: 1

Rahul Bhatnagar
Rahul Bhatnagar

Reputation: 106

You can send multiple headers one by one using the optional "replace" argument and setting it to false. So, for example, you could send

header("http://example.com/test.php");
header("POST test.php HTTP/1.1",false);

and so on. This would append to the original header.

Source : http://php.net/manual/en/function.header.php

Upvotes: -1

Related Questions