Jump to content

Parseing Xml Returns In Php


BruisedGhost

Recommended Posts

Hi all, Im relatively new to XML and have a quick question. essentially I am trying to send a xml request and format the returned data however I have no idea how to send the initial request in PHP or format the returned XML data. Im assuming I can format the returned data via CSS but the initial request how should it be sent out? I have seen several posts suggesting I use SOAP, cURL, and REST but I would rather find a simpler solution. Here is an example of my request string<code>http://api.indeed.com/apisearch??q='.&...;limit=50'; ?></code>If I manually enter the string I get my list populated with xml data. Once that is done how do I take that data and format it?Thanks!!!!!

Link to comment
Share on other sites

In PHP, you can load files using DOMDocument class.

<?php// !! Set the $title and $loc variables first !!$xmlDoc = new DOMDocument();$xmlDoc->load("http://api.indeed.com/apisearch?q={$title}&l={$loc}&limit=50");// From here on, use XML DOM to navigate, modify and then display the document however you like.// Here's an example:$list = $xmlDoc->getElementsByTagName("location");echo "<ul>";foreach($list as $obj) {  echo "<li>" . $obj->item(0)->nodeValue . "</li>";}echo "</ul>";?>

Link to comment
Share on other sites

In PHP, you can load files using DOMDocument class.
<?php// !! Set the $title and $loc variables first !!$xmlDoc = new DOMDocument();$xmlDoc->load("http://api.indeed.com/apisearch?q={$title}&l={$loc}&limit=50");// From here on, use XML DOM to navigate, modify and then display the document however you like.// Here's an example:$list = $xmlDoc->getElementsByTagName("location");echo "<ul>";foreach($list as $obj) {  echo "<li>" . $obj->item(0)->nodeValue . "</li>";}echo "</ul>";?>

Thanks IngolmeI tried the following
<?php// !! Set the $title and $loc variables first !!$title = 'Director';$loc = 'Denver';$xmlDoc = new DOMDocument();$xmlDoc->load('http://api.indeed.com/apisearch?q='.$title.'&l='.$loc.'&sort=&start=0&limit=1&filter=1&latlong=1&key=ec65abb9d28c9cabdadc9a7402b944d3&format=xml');// From here on, use XML DOM to navigate, modify and then display the document however you like.// Here's an example:$list = $xmlDoc->getElementsByTagName("location");echo "<ul>";foreach($list as $obj) {  echo "<li>" . $obj->item(0)->nodeValue . "</li>";}echo "</ul>";?>

however I am getting a

Call to undefined method DOMElement::item() in C:\Program Files\Zend\Apache2\htdocs\new.php on line 14

error, any ideas?

Link to comment
Share on other sites

The <location/> element possibly has no child nodes.The code I posted was just an example. I did not know what elements were in the document and what data you were trying to receive.

Link to comment
Share on other sites

The <location/> element possibly has no child nodes.The code I posted was just an example. I did not know what elements were in the document and what data you were trying to receive.
Here is a sample output of the xml feed, I tried it again with the root node and still no luck with the same error.
<?xml version="1.0" encoding="ISO-8859-1" ?><response>  <query>Java Developer</query>  <location>San Francisco, CA</location>  <dupefilter>true</dupefilter>  <totalresults>2315</totalresults>  <start>1</start>  <end>10</end>  <results>	<result>	  <jobtitle><![CDATA[Chief Executive]]></title>	  <company><![CDATA[ABC Corporation]]></title>	  <city>City Name</city>	  <state>ST</state>	  <country>US</country>	  <source>Job Site</source>	  <date>Thu, 13 Jan 2006 23:47:44 GMT</date>	  <snippet><![CDATA[All about my interests and hobbies]]></snippet>	  <url>http://www.domain.com/jobdetail.html?x=y&id=123</url>	  <latitude>30.287739</latitude>	  <longitude>-97.8022</longitude>	</result>	<result>	...	</result>  </results></response>

Link to comment
Share on other sites

thanks for all the help inglome but I gave up and did it in javahere is my code if it will help out anyone in the future

<html><head><title>Indeed Jobs</title></head><body><div id="jobs"></div>	<script src="http://maps.google.com/maps?file=api&v=1&key=ABQIAAAAZBHmEX-pUz26mphjVq6DsBTENWKvAmocgv4W7AlW6y4wknzddxSNvlGbpUPwhclnFklpZ8Ty4jkvSw" type="text/javascript"></script><script type="text/javascript">function sendRequest(theForm){   document.getElementById("jobs").innerHTML = "Sending...";   var url = "http://api.indeed.com/apisearch?";   url = url + "key="+URLencode(theForm.elements["key"].value);   url = url + "&q="+URLencode(theForm.elements["q"].value);   url = url + "&l="+URLencode(theForm.elements["l"].value);   url = url + "&jt="+URLencode(theForm.elements["jt"].value);   url = url + "&salary="+URLencode(theForm.elements["salary"].value);   url = url + "&radius="+URLencode(theForm.elements["radius"].value);   url = url + "&fromage="+URLencode(theForm.elements["fromage"].value);   url = url + "&limit="+URLencode(theForm.elements["limit"].value);   url = url + "&filter=1&latlong=1";   getJobs(url);}function getJobs(theUrl){   document.getElementById("jobs").innerHTML = "Sending...";   var request = GXmlHttp.create();   request.open('GET', 'proxy.php?url='+URLencode(theUrl), true);   request.onreadystatechange = function() {	  if (request.readyState == 4) {	   var jobDoc = GXml.parse(request.responseText);	 displayJobs(jobDoc);	  }   };   request.send(null);}function displayJobs(jobDoc){   var totalResultsElement = jobDoc.documentElement.getElementsByTagName("totalresults").item(0);   var numberOfJobs =  GXml.value(totalResultsElement);   var jobString = "There are "+numberOfJobs+" jobs.<br />";   var jobsElement = jobDoc.documentElement.getElementsByTagName("results").item(0);   var jobsList = jobsElement.getElementsByTagName("result");   var numberOfJobsOnThisPage = jobsList.length;   jobString = jobString + "<table>";   for (i = 0; i < numberOfJobsOnThisPage; i++){	   var thisJob = jobsList.item(i);	   var infoUrl = GXml.value(thisJob.getElementsByTagName("url").item(0));	   var jobtitle = GXml.value(thisJob.getElementsByTagName("jobtitle").item(0));	   var company = GXml.value(thisJob.getElementsByTagName("company").item(0));	   var city = GXml.value(thisJob.getElementsByTagName("city").item(0));	   var state = GXml.value(thisJob.getElementsByTagName("state").item(0));	   var country = GXml.value(thisJob.getElementsByTagName("country").item(0));	   var snippet = GXml.value(thisJob.getElementsByTagName("snippet").item(0));	   	   jobString = jobString + "<tr><td><b><a href='"+infoUrl+"'>"+jobtitle+"</a>, "+company+"</b></td>";	   jobString = jobString + "<td>"+city+", "+state+", "+country+"</td></tr>";	   jobString = jobString + "<tr><td colspan='2'>"+snippet+"</td></tr>";   }      jobString = jobString + "<tr><td colspan ='2'>Results from <a href='http://www.indeed.com'>Indeed</a></td></tr>";   jobString = jobString + "</table>";   document.getElementById("jobs").innerHTML = jobString;}function URLencode(sStr) {   return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');}</script><h1>QUERY INDEED.COM DATABASE</h1><form action="" onsubmit="sendRequest(this); return false;"><table><tr><td>Indeed key:</td><td><input type="hidden" name="key" value="--removed my API key but if you want to use this you will need to input it here--" /></td></tr><tr><td>What kind of job:</td><td><input type="text" name="q" value="" /></td></tr><tr><td>Where you want it:</td><td><input type="text" name="l" value="" /></td></tr><tr><td> Job Type:</td><td><SELECT name="jt" width:10em>		<OPTION VALUE="FT">Full Time</OPTION>		<OPTION VALUE="PT">Part Time</OPTION>		<OPTION VALUE="contract">Contract</OPTION>		<OPTION VALUE="Temp">Temp</OPTION>		</SELECT></td></tr>		<tr><td>salary:</td><td><input type="text" name="salary" value="" /></td></tr><tr><td>how far:</td><td><input type="text" name="radius" value="" /></td></tr><tr><td>how old is the post:</td><td><input type="text" name="fromage" value="" /></td></tr><tr><td>how many listings:</td><td><input type="text" name="limit" value="" /></td></tr><tr><td colspan="2"><input type="submit" value="Send" /></td></tr></table></form></body></html>

and the proxy.php file

<?$filepath = $_GET['url'];	 $file_contents = file_get_contents($filepath);	print($file_contents);  ?>

It all works quite nicely

Link to comment
Share on other sites

That's not Java, it is Javascript.It works just fine, but you can do exactly the same with PHP using XML DOM.XML DOM in PHP is just the same as in JavascriptHere's an example:

java script:  jobDoc.documentElement.getElementsByTagName("totalresults").item(0);PHP:  $jobDoc->documentElement->getElementsByTagName("totalresults")->item(0);

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...