How to make asynchronous (async) PHP calls with XMLHttpRequest. PHP doesn’t really support the asynchronous execution of PHP scripts without additional libraries, so we need a little trick.
As I mentioned earlier in my Check Website Availability with cURL post, it is pretty easy to make async PHP calls. PHP doesn’t really support the asynchronous execution of PHP scripts without additional libraries (Wayback Machine archive), so we need a little trick. That little trick is using XMLHttpRequest with Javascript.
Suppose we have a PHP script to check the online availability of our websites (e.g we rewrite a bit of the previous script):
<?php
function url_test($url) {
$timeout = 10;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
$http_respond = curl_exec($ch);
$http_respond = trim(strip_tags($http_respond));
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($http_code == "200") || ($http_code == "302")) {
return true;
} else {
// return $http_code;, possible too
return false;
}
curl_close($ch);
}
$website = $_GET["url"];
if(!url_test($website)) { echo $website ." is down!"; }
else { echo $website ." functions correctly."; }
?>
Don’t set curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );!
We can call this script asynchronously by adding an extra PHP script, let’s call it proces.php.
<?php
/**
* Partially taken from ChipmunkNinja:
* http://www.chipmunkninja.com/Troubles-with-Asynchronous-Ajax-Requests-g@
*
* PHP XMLHttpRequest async calls, part 1
*/
session_start();
?>
<html>
<head>
<meta charset="utf-8" />
<title>PHP/XMLHttpRequest async calls, part 1</title>
</head>
<script>
/**
* Create a new XmlHttpRequest object.
*/
function getNewHTTPObject()
{
var xmlhttp;
/** Special IE only code ... */
/*@cc_on
@if (@_jscript_version >= 5)
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (E)
{
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
/** Every other browser on the planet */
if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp = false;
}
}
return xmlhttp;
}
/**
* We call this function when the page loads, and it starts
* asynchronous Ajax requests to our check.php script.
*/
var xml1;
var xml2;
function onLoadFunction()
{
xml1 = getNewHTTPObject();
xml2 = getNewHTTPObject();
xml1.open('GET', 'http://www.saotn.org/check.php?url=saotn.org', true);
xml2.open('GET', 'http://www.saotn.org/check.php?url=code-snippets.nl', true);
xml1.onreadystatechange = handleResponse1;
xml2.onreadystatechange = handleResponse2;
xml2.send(null);
xml1.send(null);
}
/**
* Response 1
* This handles the response from the first ajax request.
* It puts the returned string in the <div> element
* with the id 'checkresult'.
*/
function handleResponse1()
{
if (xml1.readyState == 4)
{
document.getElementById('checkresult').innerHTML =
xml1.responseText;
}
}
/**
* Response 1
* This handles the response from the second ajax request.
*/
function handleResponse2()
{
if (xml2.readyState == 4)
{
document.getElementById('checkresult2').innerHTML =
xml2.responseText;
}
}
</script>
<body onLoad='onLoadFunction();'>
<div id='checkresult'>Waiting for response from web servers</div>
<div id='checkresult2'>Waiting for response from FTP servers</div>
</body>
</html>
By calling http://www.saotn.org/process.php, two PHP requests are executed, asynchronously! The output is for example:
saotn.org functions correctly. code-snippets.nl functions correctly.
Be aware, this code is old and partialy taken from ChipmunkNinja (Wayback Machine archive).