Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP post请求中添加xmlhttprequest_Php_Ajax - Fatal编程技术网

在PHP post请求中添加xmlhttprequest

在PHP post请求中添加xmlhttprequest,php,ajax,Php,Ajax,我有一个文件,它使用PHP向其他服务器内的文件发送POST请求: // 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',

我有一个文件,它使用PHP向其他服务器内的文件发送POST请求:

// 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);
我请求的文件检查
HTTP\u X\u REQUESTED\u WITH
是否等于
xmlhttprequest
。(之所以这样做是因为该文件发送的大多数请求都是通过ajax发送的)


那么,如何在PHP代码中添加此类内容呢?要发送此参数?

您可以将X_Requested_With header添加到http数组的头中,如下所示:

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