Cross Domain Ajax Handler Using PHP

 Presentation of a PHP-based solution to cross-domain AJAX requests.  

References:
There are ways to handle AJAX requests on the same domain, such as: 
http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/
http://stackoverflow.com/questions/4106993/ajax-cross-sub-domain-requests
http://00f.net/2008/06/24/cross-subdomain-ajax-made-simple/

Or using IFRAMES to allow for cross-domain reqeusts, such as:
http://abhinavsingh.com/blog/2009/11/making-cross-sub-domain-ajax-xhr-requests-using-mod_proxy-and-iframes/
http://stackoverflow.com/questions/648899/a-question-about-cross-domain-subdomain-ajax-request

The following:
uses an intermediate PHP script as buffer.
maintains the integrity of the browser domain, while allowing for cross-domain script access.  


[notice]<!  —   example.html   — !>[/notice]
<script type=”text/javascript”>

// http://code.google.com/p/json-simple/wiki/JSPAndAJAXExamples
function createXMLHttpRequest()
{

  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject(“Msxml2.XMLHTTP”); }
  catch (e) { try { xmlhttp = new ActiveXObject(“Microsoft.XMLHTTP”); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == “GET”)
      {
        xmlhttp.open(sMethod, sURL+”?”+sVars, true);
        sVars = “”;
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader(“Method”, “POST “+sURL+” HTTP/1.1″);
        xmlhttp.setRequestHeader(“Content-Type”,
          “application/x-www-form-urlencoded”);
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function example()
{
//CALL LOCAL AJAX
file = “/local.php”;

//CREATE CONNECTION
var request = createXMLHttpRequest();
if ( !request ) {
   alert( request )
return false;
}

//DEFINE HANDLER METHOD
var callback = function( oXML ) {
alert(oXML.responseText);
}

//RUN SCRIPT
request.connect(
file,
   ‘POST’,
  ‘var=’ + val,
   callback
   );
return;
}
</script>
<! —   END example.html  —  !>

[notice]<! —   local.php  —  !>[/notice]
<?php
class local {
public function local() {
//CALL FUNCTION ON ANOTHER DOMAIN
$url = “http://some.other.domain/remote.php?var=”.$_POST[‘var’];

//BUFFER
ob_start();
readfile($url);
$content = ob_get_contents();
ob_end_clean();

//RETURN to example.html
echo $content;
}
}
$local = new local();
?>
<! —   END local.php  —  !>

[notice]<! —   remote.php  —  !>[/notice]
<?php
class remote {
  public function remote() {
  //RETURN to local.php
echo $_GET[‘var’];
}
}
$remote = new remote();
?>
<! —   END remote.php  —  !>

, football anyone? 
Posted in How To