26282 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Win a Nexus 7 with
reed.co.uk and The
Register
WTF is... LTE
Advanced?
"Lab-smashing"
Stuxnet HELPED
Iran"s nuke effort,
says brainiac
MYSTERY Nokia Lumia
with
gazillion-pixel
camera "spotted"
Machine learning
climbs atop Hadoop
New 4TB drive
spaffs half a telly
season into your
eyes AT ONCE
Buff American
beauties keen to
dominate Euro youth
in tech tussle
Indian "attacks"
Norwegian telco to
get at Pakistan,
China
German robots sent
to Oz to make GPS
millimetre-perfect
Dell"s
PC-on-a-stick
landing in July:
report
Slashdot
German Researchers
Hit 40 Gbps On
Wireless Link
The Hunt For
LulzSec"s Missing
Sixth Member
Latvian Police Raid
Teacher"s Home for
Uploading $4.00
Textbook
EFF Resumes
Accepting Bitcoin
Donations After Two
Year Hiatus
Google Drops XMPP
Support
Motion To Delay
Sanctions Against
Prenda Lawyers
Denied
NSA Data Center the
Focus of Tax
Controversy
Viruses In Mucus
Protect From
Infection
Reporters
Threatened, Labeled
Hackers For Finding
Security Hole
Judges Debate
Patents and If New
Software Makes a
Computer a "New
Machine"
Article viewer

Cross site AJAX



Written by:MaxMouse
Published by:MaxMouse
Published on:2009-10-29 10:18:42
Topic:PHP
Search OSI about PHP.More articles by MaxMouse.
 viewed 10319 times send this article printer friendly

Digg this!
    Rate this article :
While developing across several domains I found AJAX lacking, in that I could only request an XML document from the same domain the script was currently executing from. Since I required some data to be shared across several domains I ended up with duplicate XML documents, updating them meant updating the master local XML document then uploading it to all of the different servers one after another. The following is a demonstration on cross site AJAX using a PHP proxy, pAJAX anyone?

AJAX, Asynchronous JavaScript and XML is a fancy way of saying "Make the browser retrieve and parse some XML", the important line of (JavaScript) code i want to talk about here is:

request.open("GET", "some-file-on-the-server.xml", true);


In my case i was using it to process an XML file containing data for google maps, the XML document is in the following format:

<agents lat="" lng="" company_name="" individual_name="" additional_info="" phone="" mobile_phone="" fax="" email="" url="" />


As outlined in the summary what I needed to do was this:

request.open("GET", "http://www.some-site.tld/some-file-on-the-server.xml", true);


Currently only firefox (>3.5) supports this kind of thing with minor changes in XML and code, but since I had to maintain browser support for the big 5 I couldn't use this method.

What I decided to do was keep one XML document on one server then use PHP to handle the rest on all other servers, lets say our XML document is at http://www.max.com/document.xml, for each site i would like that to be available I used the following.

AJAX Request:
request.open("GET", "cross_site_xml_engine.php", true);


cross_site_xml_engine.php - code:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Content-type: text/xml");
echo "<!--Cross site XML Engine: Written by MaxMouse\nThe below contents exist at http://www.max.com/document.xml\n-->\n";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.max.com/document.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);

echo $contents;
?>


So now, on each server i have the above code, the AJAX request is happy because I'm requesting a file that resides on the same domain, as you can see the PHP code uses cURL to get the data from http://www.max.com/document.xml, it changes the header to XML and simply outputs the result, as far as the AJAX (JavaScript) request is concerned cross_site_xml_engine.php is simply an XML file. Whenever i update http://www.max.com/document.xml all other servers echo the change instantly without me having to re-upload to each of them, and the browser won't cause inconsistency with caching issues because I have modified the cache-control header.

Did you like this article? There are hundreds more.

Comments:
Domuk
2009-10-30 20:45:55
Depending on the need for it to be in XML, you could instead store it in JSON and load it as a JS file - an AJAX proxy is a nice alternative though, especially if it caches.
MaxMouse
2009-11-02 09:07:12
I never really looked into loading it as a .JS file, i was using stock code from google which just looped through an XML file, modifying it this way allowed me to not touch the standard code. As for caching, i didn't want that to happen (Hence the header change) because if i edit the XML and the browser uses cache it won't be reflected in the end google map.
Domuk
2009-11-02 20:35:20
I mean caching on the server-side, not browser caching - if the site gets big, you don't want every hit to also hit another server - especially if the XML changes less than every ten minutes or so. I see that as being the main advantage of using a PHP script as a proxy, really, since I'd expect the curl hit to be the majority of the request time. Also would let you carry on regardless if the other site went down.

As for modifying the standard code, if the argument to the function is a URL then it seems odd but no, nothing you can do - if you're handling the AJAX yourself, then you can just load the JSON'd XML structure at the time when you'd do the AJAX request. All something of a moot point when you've got a working solution though!
MaxMouse
2009-11-03 09:19:35
I see. The XML doesn't change that often, every couple of days maybe, and if the host server dies, it's only a Google map, it isn't critical data. If it where critical I suppose i could also have run a PHP cron job to periodically download the XML document and save it, then use the AJAX request as standard.

Have you looked at Firefox's AJAX cross site AJAX support?
Domuk
2009-11-04 00:07:15
No, I haven't. It was a sin when IE added its own tags back in the day, now that's the norm for other browsers. But now Firefox adds functionality that would actually break apps for non-Firefox users? I dread to think of new developers not understanding the issue and building their first app around it.
MaxMouse
2009-11-06 09:36:07
I never use IE, i still have to code support for it though, IE doesn't even pass the AcidTest (http://www.acidtests.org/) MS have known this since IE6, currently i have the big five installed (IE, FF, Chrome, Opera, and Safari) for testing and all but IE pass the acid test, so i have little time for it. Both firefox and Opera have their own "Special" support for things it's completely insane.

Also, i do hope no one falls into the trap of using functionality which is supported on one client but nothing else.
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
elasolova
My PHP Projects on Sat 26th Sep 10am
I have been developing PHP applications for almost a year now. I have developed three projects. One is a simple trivia game. The other is a question-answer based community at http://www.javaist.com/quans . The last one is a programming challenge site just
countll
Blog entry for Thu 25th Oct 7am on Thu 25th Oct 7am
soo nu on this wicked world of NET. just decided to dive in today..hope friend aroun here can help

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Test of experience (hopefully) by AcidIce

Things you're only likely to know if you've actually written a lot of PHP before :)


     
Your Ad Here
 
Copyright Open Source Institute, 2006