If 1st XML is empty then load a different one - php

Im getting the contents of an xml feed and printing the titles on my web page with php:
$url = 'http://site.com/feed';
$xml = simplexml_load_file($url);
foreach($xml->ART as $ART) {
echo $ART->TITLE;
}
I want to be able to set a backup, so if the first xml isn't found a different one is loaded instead.
I tried the following code but it doesn't work. If the feed isnt found the page shows 'XML Parsing Error:' which i guess isnt the same as nothing.
if ($url != '') {
$xml = simplexml_load_file($url);
} else {
//Here I would load a different xml file.
}
What should I do? Should I write conditional php to check if the first url contains a TITLE, and if not load the 2nd url?
Thanks
UPDATE
This messed up my whole page:
$first_url = 'http://site.com/feed1';
$second_url = 'http://site.com/feed2';
// if URL wrappers is enabled
if (is_url($first_url))
{
// parse first url
$xml = simplexml_load_file($first_url);
}
else
{
// parse second url
$xml = simplexml_load_file($second_url);
}
foreach($xml->ART as $ART) {
echo $ART->TITLE;
}

See simplexml_load_file
Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.
Example from php.net
<?php
// The file test.xml contains an XML document with a root element
// and at least an element /[root]/title.
if (file_exists('test.xml')) {
$xml = simplexml_load_file('test.xml');
print_r($xml);
} else {
exit('Failed to open test.xml.');
}
?>
EDIT: you can do
$url = 'http://site.com/feed';
if( $xml = simplexml_load_file($url) ) {
foreach($xml->ART as $ART) {
echo $ART->TITLE;
}
} else {
//parsing new url
}

function parse_xml($url)
{
// your code
}
try
{
parse_xml($first_url);
}
catch (Exception $e)
{
parse_xml($second_url);
}
Alternatively, you can do a check if the URL return XML before proceed to parsing :-
// if URL wrappers is enabled
if (is_url($first_url))
{
// parse first url
$xml = simplexml_load_file($first_url);
}
else
{
// parse second url
$xml = simplexml_load_file($second_url);
}

Think ive got it working with:
$url = 'site.com/feed1';
$xml = simplexml_load_file($url);
if ($xml == null) {
$url = 'site.com/feed2';
$xml = simplexml_load_file($url);
}
foreach($xml->ART as $ART) {
echo $ART->TITLE;
}

Related

altering text returned from source

This code pulls the name of a shipping option from Canada Post's server. I need to somehow specify that if it comes back as "Expedited" it should say "Canada Post" instead. Is that possible?
$name = substr(
$resultXML,
strpos($resultXML, "<name>") + strlen("<name>"),
strpos($resultXML, "</name>") - strlen("<name>") - strpos($resultXML, "<name>")
);
You can use a simple if statement:
if ($name === 'Expedited') {
$name = 'Canada Post';
}
You need to use simple xml parser which is built into php
// make it more error friendly
$saved = libxml_use_internal_errors(true);
//load the xml as object
$xml = simplexml_load_string($inputString);
if ($xml === false) {
foreach (libxml_get_errors() as $error) {
echo $error->message, "\n";
}
libxml_use_internal_errors($saved);
return;
}
libxml_use_internal_errors($saved);
echo $xml->asXML(); //you can see the XML here
get the name as per object position like $xml->obj1->name etc

Simple DOM file_get_html returns empty page

I'm having trouble with passing a complex url to file_get_html When I try this code
<?php
require_once("$_SERVER[DOCUMENT_ROOT]/dom/simple_html_dom.php");
$base = $_GET['url'];
//file_get_contents() reads remote webpage content
$html_base = file_get_html("http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927");
foreach($html_base->find('p') as $td) {
echo $td;
}
?>
It works
But if I try to pass the url as a variable via mysite.com/goget.php?url=http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927
<?php
require_once("$_SERVER[DOCUMENT_ROOT]/dom/simple_html_dom.php");
$base = $_GET['url'];
//file_get_contents() reads remote webpage content
$html_base = file_get_html($base);
foreach($html_base->find('p') as $td) {
echo $td;
}
?>
It returns a blank page.
Any help?
Use urlencode():
"mysite.com/goget.php?url="
.urlencode("http://www.realestateinvestar.com.au/ME2/dirmod.asp?sid=1A0FFDB3E8CD48909120C118D03F6016&nm=&type=news&mod=News&mid=9A02E3B96F2A415ABC72CB5F516B4C10&tier=3&nid=C67A9DD2C0144B9EB41DB58365C05927")

Replacing link with plain text with php simple html dom

I have a program that removes certain pages from a web; i want to then traverse the remaining pages and "unlink" any links to those removed pages. I'm using simplehtmldom. My function takes a source page ($source) and an array of pages ($skipList). It finds the links, and I'd like to then manipulate the dom to convert the element into the $link->innertext, but I don't know how. Any help?
function RemoveSpecificLinks($source, $skipList) {
// $source is the html source file;
// $skipList is an array of link destinations (hrefs) that we want unlinked
$docHtml = file_get_contents($source);
$htmlObj = str_get_html($docHtml);
$links = $htmlObj->find('a');
if (isset($links)) {
foreach ($links as $link) {
if (in_array($link->href, $skipList)) {
$link->href = ''; // Should convert to simple text element
}
}
}
$docHtml = $htmlObj->save();
$htmlObj->clear();
unset($htmlObj);
return($docHtml);
}
I have never used simplehtmldom, but this is what I think should solve your problem:
function RemoveSpecificLinks($source, $skipList) {
// $source is the HTML source file;
// $skipList is an array of link destinations (hrefs) that we want unlinked
$docHtml = file_get_contents($source);
$htmlObj = str_get_html($docHtml);
$links = $htmlObj->find('a');
if (isset($links)) {
foreach ($links as $link) {
if (in_array($link->href, $skipList)) {
$link->outertext = $link->plaintext; // THIS SHOULD WORK
// IF THIS DOES NOT WORK TRY:
// $link->outertext = $link->innertext;
}
}
}
$docHtml = $htmlObj->save();
$htmlObj->clear();
unset($htmlObj);
return($docHtml);
}
Please provide me some feedback as if this worked or not, also specifying which method worked, if any.
Update: Maybe you would prefer this:
$link->outertext = $link->href;
This way you get the link displayed, but not clickable.

PHP SimpleXML Breaking when trying to traverse nodes

I'm trying to read the xml information that tumblr provides to create a kind of news feed off the tumblr, but I'm very stuck.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts[0] AS $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
Which always breaks as soon as it tries to go through the nodes. So basically "tumblr->posts;....ect" is displayed on my html page.
I've tried saving the information as a local xml file. I've tried using different ways to create the simplexml object, like loading it as a string (probably a silly idea). I double checked that my webhosting was running PHP5. So basically, I'm stuck on why this wouldn't be working.
EDIT: Ok I tried changing from where I started (back to the original way it was, starting from tumblr was just another (actually silly) way to try to fix it. It still breaks right after the first ->, so displays "posts[0] AS $post....ect" on screen.
This is the first thing I've ever done in PHP so there might be something obvious that I should have set up beforehand or something. I don't know and couldn't find anything like that though.
This should work :
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if ( !$xml ){
exit('Failed to retrieve data.');
}else{
foreach ( $xml->posts[0] AS $post){
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo $title;
echo '<p>'.$small_post.'</p>';
echo '<hr>';
}
}
First thing in you code is that you used root element that should not be used.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
$xml->posts returns you the posts nodes, so if you want to iterate the post nodes you should try $xml->posts->post, which gives you the ability to iterate through the post nodes inside the first posts node.
Also as Needhi pointed out you shouldn't pass through the root node (tumblr), because $xml represents itself the root node. (So I fixed my answer).

simplexml load on google weather api prooblem

Hi I have been having problems with the google weather api having errors Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 2: parser error ....
I tried to use the script of the main author(thinking it was my edited script) but still I am having this errors I tried 2
//komunitasweb.com/2009/09/showing-the-weather-with-php-and-google-weather-api/
and
//tips4php.net/2010/07/local-weather-with-php-and-google-weather/
The weird part is sometimes it fixes itself then goes back again to the error I have been using it for months now without any problem, this just happened yesterday. Also the demo page of the authors are working but I have the same exact code any help please.
this is my site http://j2sdesign.com/weather/widgetlive1.php
#Mike I added your code
<?
$xml = file_get_contents('http://www.google.com/ig/api?weather=jakarta'); if (! simplexml_load_string($xml)) { file_put_contents('malformed.xml', $xml); }
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=jakarta');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
and made a list of the error but I can't seem to see the error cause it's been fixing itself then after sometime goes back again to the error
here is the content of the file
<?php include_once('simple_html_dom.php'); // create doctype $dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes //header("Content-Type: text/plain");
// create root element
$xmlProducts = $dom->createElement("products");
$dom->appendChild($xmlProducts);
$pages = array( 'http://myshop.com/small_houses.html', 'http://myshop.com/medium_houses.html', 'http://myshop.com/large_houses.html' ) foreach($pages as $page) { $product = array(); $source = file_get_html($page); foreach($source->find('img') as $src) { if (strpos($src->src,"http://myshop.com") === false) { $product['image'] = "http://myshop.com/$src->src"; } } foreach($source->find('p[class*=imAlign_left]') as $description) { $product['description'] = $description->innertext; } foreach($source->find('span[class*=fc3]') as $title) { $product['title'] = $title->innertext; } //debug perposes! echo "Current Page: " . $page . "\n"; print_r($product); echo "\n\n\n"; //Clear seperator } ?>
When simplexml_load_string() fails you need to store the data you're trying to load somewhere for review. Examining the data is the first step to diagnose what it causing the error.
$xml = file_get_contents('http://example.com/file.xml');
if (!simplexml_load_string($xml)) {
file_put_contents('malformed.xml', $xml);
}

Categories