Home > Archive > PHP Language > December 2006 > Combining XML, using SimpleXML
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Combining XML, using SimpleXML
|
|
|
| I'm trying to use the Yahoo Search API to get a list of URLs for a site in
the Yahoo index.
I can only get 100 results at a time, but I would like 1000 total (the
maximum), so I want to make 10 queries, each time starting at 100 places
higher. So the first query would get results 1 to 100, the second would
start at 101, and so on.
I'm getting a blank page. Everything works until I try to combine the 10
requests into one XML file, and then I get a blank page. I think it is
because I am trying to make a big XML file out of smaller ones, and
including the entire smaller XML files into the big file.
<?php
// Capture the URL input in the form on the previous page
$url = $_GET['url'];
// makes 10 queries to Yahoo's Search API starting at results 1-100 and
incrementing by 100 on each loop
for($i = 1; $i < 1000; $i += 100) {
$request =
'http://search.yahooapis.com/SiteExplorerService/V1/pageData?appid=useridhere&query='.urlencode($url).'&results=100&start='.$i;
$response = file_get_contents($request);
$xml[$i] = new SimpleXMLElement($response);
}
// combine all arrays of $xml[i] (all 10 requests) into a new string called
$all_xml.
// The problem here is that the new string doesn't just contain <Result>s
from the XML,
// it includes the entire XML file which I think is what is making it fail
foreach ($xml as $value) {
$all_xml .= $value;
}
// Send output
$j = 1;
foreach ($all_xml->Result as $Result) {
echo "#$j: <a href=\"" . $Result->Url ."\">" . $Result->Url . '</a><br
/>';
$j++;
}
| |
|
| I forgot to mention, the format that Yahoo returns is something like this:
<Result>
<Url>http://www.example.com/</Url>
<!-- some more here -->
</Result>
|
|
|
|
|