如何使用PHP模拟XMLHttpRequest客户端?

tpu*_*nen 2 php ajax xmlhttprequest

我正在寻找一个如何使用PHP模拟XMLHttpRequest客户端的示例.

换句话说,通过HTTP POST消息发送请求,并接收和处理回调消息.

Bol*_*wyn 7

如果您想"真正"模拟AJAX请求,您应该与上述所有解决方案一起考虑将此标头与您的请求一起发送:

X-Requested-With: XMLHttpRequest
Run Code Online (Sandbox Code Playgroud)

(查看解决方案的手册如何设置自定义标题).原型,jQuery,mootools等都是在通过AJAX请求数据时发送此标头.


Rag*_*geZ 6

你可以使用卷曲来达到这个目的

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set the post 
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array( 'foo' => 'bar'));

// grab URL and pass it to the browser
$result = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
var_dump($result);
Run Code Online (Sandbox Code Playgroud)