Lacrifilm Lacrifilm - 1 year ago 99
Ajax Question

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
is equal to
. (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?

Answer Source

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",
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download