PHP RSS parser - - not working - php

Hello I'm working on parsing this feed and cannot seem to get it to work
<?php
$url = "http://insite.unthsc.edu/dailynews/category/campus-news/feed/";
$xml = simplexml_load_file($url);
print_r($xml);
?>
I get this currently when I output pre
I've done this before for other feeds and xml files but cannot for the life of me figure out why it is not working this time.
Thanks

The problem is right in the error message: you need to authenticate before loading the XML resource.
Open the link in an Incognito window, or
curl http://insite.unthsc.edu/dailynews/category/campus-news/feed/
and you'll see what PHP sees.

Related

PHP - file_get_html not returning anything

I am trying to scrape data from this site, using "inspect" I am checking the class of the div, but when I try to get it, it doesn't display anything:
Trying to get the "Diamond" below "Supremacy".
What I am using:
<?php
include('simple_html_dom.php');
$memberName = $_GET['memberName'];
$html = file_get_html('https://destinytracker.com/d2/profile/pc/'.$memberName.'');
preg_match("/<div id=\"dtr-rating\".*span>/", $html, $data);
var_dump($data);
?>
FYI, simple_html_dom is a package available on SourceForge at http://simplehtmldom.sourceforge.net/. See the documentation.
file_get_html(), from simple_html_dom, does not return a string; it returns an object that has methods you can call to traverse the HTML document. To get a string from the object, do:
$url = https://destinytracker.com/d2/profile/pc/'.$memberName;
$html_str = file_get_html($url)->plaintext;
But if you are going to do that, you might as well just do:
$html_str = file_get_contents($url);
and then run your regex on $html_str.
BUT ... if you want to use the power of simple_html_dom ...
$html_obj = file_get_html($url);
$the_div = $html_obj->find('div[id=dtr-rating]', 0);
$inner_str = $the_div->innertext;
I'm not sure how to do exactly what you want, because when I look at the source of the web link you provided, I cannot find a <div> with id="dtr-rating".
My other answer is about using simple_html_dom. After looking at the HTML doc in more detail, I see the problem is different than I first thought (I'll leave it there for pointers on better use of simple_html_dom).
I see that the web page you are scraping is a VueJS application. That means the HTML sent by the web server causes Javascript to run and build the dynamic contents of the web page that you see displayed. That means, the <div> your are looking for with regex DOES NOT EXIST in the HTML sent by the server. Your regex cannot find anything but its not there.
In Chrome, do Ctl+U to see what the web server sent (no "Supremacy"). Do Ctl+Shift+I and look under the "Elements" tab to see the HTML after the Javascript has done is magic (this does have "Supremacy").
This means you won't be able to get the initial HTML of the web page and scrape it to get the data you want.

PHP not parsing XML in expected format?

Im trying to pull this data into PHP, ultimately get it into Javascript so I can make some graphs.
When I download the data using cURL from my mac terminal, I open it in xCode and it looks exactly as expected. No issues accessing the website for data:
curl "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/‌​XmlView.aspx?data=yieldyear&year=2015" > test.xml
open test.xml
When I try to pull into PHP, the xml looks very different. For example, the d:BC_1MONTH tag just isn't present:
$url = "http://www.treasury.gov/resource-center/data-chart-center/interest-rates/pages/XmlView.aspx?data=yieldyear&year=2015";
$xml = simplexml_load_file($url);
print_r($xml);
How do i use php to get XML data in the same format as it is on the website and with cURL download?
print_r() doesn't work as expected with SimpleXMLElement objects. You should do this instead: echo $xml->asXML()
When I did that with your code above I saw the element you were asking about.

How to Parse XML from a URL using PHP

Im learning how to parse XML elements into an html document.
This takes a url with an xml, reads the elements but it ain't working...also
I want to take it a bit further but I simply haven't been able to, how can I make it so I read the xml from a url? and use an xml element as filename to create an html document using a template?
////EDIT this is what I tried! /////EDIT/////EDIT/////EDIT/////EDIT/////EDIT/////EDIT
I tried this just for the sake of me knowing what Im doing(...apparently nothing haha) so I could echo if the information was right....
<?php
$url = "http://your_blog.blogspot.com/feeds/posts/default?alt=rss";
$xml = simplexml_load_file($url);
print_r($xml);
?>
Thank you for your time!
Generally, "cross-domain" requests would be forbidden by web browsers, per the same origin security policy.
However, there is a mechanism that allows JavaScript on a web page to make XMLHttpRequests to another domain called Cross-origin resource sharing (CORS).
Read this about CORS:
http://en.wikipedia.org/wiki/Cross-origin_resource_sharing
Check this article out about RSS feeds:
http://www.w3schools.com/php/php_ajax_rss_reader.asp

php simple_load_string always returning zero data on well formed xml docs

I have been trying to load a number of XML files into a PHP page using a simple load string. Strange as this might sound, not one loads, and all return as empty.
I can load them using $xmldoc = new DomDocument( '1.0' ); and I can view all directly in the browser. I'm getting really frustrated because I can't see why they should fail to load.
I am using offline development - Win7 proff, XAMPP Version: 1.8.1 with PHP 5.4 loaded.
Example of my simple loading code:
$xml = simplexml_load_string('menu45.xml');
if ( !$xml ) {
echo "empty!!!!";
// return 'Error';
}
I am hoping someone will let me know if there is a problem with using a simple load string on a Windows machine or if there's a bug in PHP 5.4, or XAMPP has issues with it, or something.
I've Google'd and read everything I could for an 18 hour stint without joy. Any suggestion would be greatly appreciated.
simplexml_load_string() loads strings, not files.... and "menu45.xml" isn't a well formed xml string. Try simplexml_load_file() instead.

why this error `String could not be parsed as XML` is seen when tried to create a object of simplexmlelement?

I have a functionality like below and getting an error String could not be parsed as XML
$category_feed_url = "http://www.news4u.com/blogs/category/articles/feed/";
$file = file_get_contents($category_feed_url);
$xml = new SimpleXMLElement($file);
foreach($xml->channel->item as $feed)
{
echo $feed->link;
echo $feed->title;
...
why this error has occurred.
The URL points to an HTML document.
It is possible for a document to be both HTML and XML, but this one isn't.
It fails because you are trying to parse not-XML as if it was XML.
See How to parse and process HTML with PHP? for guidance in parsing HTML using PHP.
You seem to be expecting an RSS feed though, and that document doesn't resemble one or reference one. The site looks rather spammy, possibly that URI used to point to an RSS feed but the domain has now fallen to a link farm spammer. If so, you should find an alternative source for the information you were collecting.
"String could not be parsed as XML", your link is an html page.

Categories