I m looking for an example how to emulate XMLHttpRequest client using PHP.
In other words, send the request over HTTP POST message, and receive and process the callback message.
I m looking for an example how to emulate XMLHttpRequest client using PHP.
In other words, send the request over HTTP POST message, and receive and process the callback message.
server.php:
<?php var_dump($_POST);
client.html:
<html>
<head>
<title>omg</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.post(
"server.php"
, {omg: "wtf"}
, function (data) { alert(data); }
);
});
</script>
</head>
<body></body>
</html>
edit: ok, so it s http client written in PHP!
<?php
$r = new HTTPRequest("server.php", HTTP_METH_POST);
$r->addPostFields(array( omg => wtf ));
$r->send();
var_dump($r->getResponseCode());
var_dump($r->getResponseBody());
If you want to really simulate an AJAX request, you should, together with all of the above solutions, consider sending this header with your request:
X-Requested-With: XMLHttpRequest
(check the manuals to the solutions how to set custom headers). Prototype, jQuery, mootools and the such all send this header when they request data via AJAX.
you can use curl for that purpose
<?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);
The easiest method would be the command line tool curl
, especially if you have a sample of the data you want to post.
I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...
I m trying to find a reusable way to set focus from one text box to another upon enter using ASP.NET, but using client-side JavaScript to do so. The only reason I mention this is to be done in ASP....
I have some Javascript JQuery code that does an Ajax call to the server every 5 mins, it s to keep the server session alive and keep the user logged in. I m using $.ajax() method in JQuery. This ...
Why are my AJAX requests failing? I have a website www.foo.com, and I m running Tomcat on the same server, www.foo.com:8080/services. However, when a file on foo.com makes an ajax call as such: $....
I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...
I have a entry form. Below it, I want to show a grid containing existing records. As the user clicks on a row of the grid, the values must get filled in the fields of the form above. Is there any way ...
I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...
Ok, I m stumped. Basically, this script works fine in FF, but not in IE (6,7 or 8) - in which it returns an "Object doesn t support this property or method" error. Any ideas?: function ...