Lacrifilm
Lacrifilm

Reputation: 283

Add xmlhttprequest inside a PHP post request

I have this file that send a POST request with PHP to a file inside other server:

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

The file that I'm requesting checks if HTTP_X_REQUESTED_WITH is equal to xmlhttprequest. (this is done because most of the requests sent by that file are through ajax)

So how can I add this kind of thing in my PHP code? to be sent this parameter?

Upvotes: 1

Views: 472

Answers (1)

hazelnoot
hazelnoot

Reputation: 86

You can add the X_Requested_With header to the headers of the http array like so:

'header'  => "Content-type: application/x-www-form-urlencoded\r\n" .
             "X-Requested-With: xmlhttprequest\r\n",

Upvotes: 2

Related Questions