views:

303

answers:

2

I am trying to load Google custom XML search results into a PHP page and then manipulate the XML using JQuery.

I understand that I can't use javascript to grab the XML file as it's on an external domain but can I use PHP to grab the XML from the server and then make it available to JQuery as an XML file, or other DOM structure, so I can then traverse that XML using, for example:

$(resultXml).find("R").each(function(){
    $("ul.results").append("<li>"+$(this).find("S")+"</li>");
});

where resultXml is the XML object.

The XML url is something like this:

http://www.google.com/cse?cx=XXXXXXX&amp;client=google-xxxx&amp;output=xml_no_dtd&amp;q=$keyword

Where the $keyword is passed to the PHP page in the GET string.

I'm good with the front-end stuff but less so with server-side (which is why I'm not parsing the XML in PHP) and I'm puzzled how I would go about grabbing the XML in PHP and converting that into a form JQuery can parse (using json_encode perhaps?). I have read about several ways of including an external file in PHP using functions like fopen but there seem to be a lot of caveats about proxies and permissions.

Thanks.

+2  A: 

This'll get you headed on the right track:

<?php
echo file_get_contents("http://www.google.com/cse?cx=XXXXXXX&amp;client=google-xxxx&amp;output=xml_no_dtd&amp;q=$keyword");

You can read more on jQuery and XML here.

Matthew Scharley
Thanks for the start Matthew!
Marcus
+1  A: 

in php you can get your data into a json formatted string using

<?php
   $json = json_encode(file_get_contents("http://www.google.com/cse?cx=XXXXXXX&amp;client=google-xxxx&amp;output=xml_no_dtd&amp;q=$keyword"));
?>

then assign it to a javascript variable

<script>var json = <?= $json; ?></script>

now use jQuery to do whatever you want with the json variable

mbehan
I used this and have the json object being parsed by the JQuery ok but it doesn't work in IE. I'm using the following code to examine the data:$(gXml).find("R").each(function(){ resultUrl = $(this).find("U").text(); $("ul.results").append("<li>"+$(this).find("T").text()+"</li>");});Any reason why IE wouldn't like this? In FF, Safari, etc it prints out the correct data.Is there a way to keep the XML as XML?
Marcus
you can keep it in xml if you like, just remove the json_encode part and then you can parse the xml with jQuery (decent example here: http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery). The code for parsing the json should work fine in IE8 if its working in other browsers though. Maybe check for errors in your html markup, if you are using xhtml strict doctype and are missing a closing tag for your ul for example IE8 might choose not to display it
mbehan
Thanks for the clarification. I did go over my code a hundred times and could find nothing wrong. Luckily (as I was tearing my hair out) I stumbled across this jquery bug - http://dev.jquery.com/ticket/3143 - that requires the object be passed through a constructor before IE6/7 will recognise it as such. Now it works perfectly!Cheers.
Marcus