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