I'm trying to get the PS4 firmware version from their XML, but for some reason it's returning NULL.
<?php
$list = simplexml_load_file('http://feu01.ps4.update.playstation.net/update/ps4/list/eu/ps4-updatelist.xml');
if($list) {
echo $list->system_pup[0]['label']; // get firmware version
} else {
echo 'Error opening the XML file.';
}
?>
I have no idea what I'm doing wrong, because I've followed this article and it seems I've done it correctly.
Any ideas?
If accessing the wrong element simplexml doesn't throw an error it just gives you the nothingness that your call returned. You should look at the structure to determine where in the structure your element is. In this case you are off by 1 element.
$list = simplexml_load_file('http://feu01.ps4.update.playstation.net/update/ps4/list/eu/ps4-updatelist.xml');
if($list) {
//print_r($list);
echo $list->region->system_pup[0]['label']; // get firmware version
} else {
echo 'Error opening the XML file.';
}
Another option can be accessing attributes of a node with attributes() function:
$list = simplexml_load_file('http://feu01.ps4.update.playstation.net/update/ps4/list/eu/ps4-updatelist.xml');
echo $list->region->system_pup->attributes()->label;
Related
<span class="contact-seller-name">Enda</span>
Now I want to echo 'Enda' inside this span tag using php
Here's my php code
$url="http://website.example.com";
$html = file_get_html( $url );
$value = $html->find('span.contact-seller-name');
echo $value->innertext;
From their documentation it looks like find returns an array of found values matching filter parameters:
From:
http://simplehtmldom.sourceforge.net/
Code:
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
They also provide another example for getting a specific element:
$html->find('div[id=hello]', 0)->innertext = 'foo';
So my guess would be something like this will get you want you desire:
$value = $html->find('span.contact-seller-name', 0);
echo $value->innertext;
By adding the 0 as a parameter it returns the first found instance of that filter.
Take a look at their API here:
http://simplehtmldom.sourceforge.net/manual_api.htm
It describe what the find method returns (an array of element objects or element object if the second parameter is defined)
Then using any of the provided methods for the element object you can get the desired text.
Full working example tested on a live site:
$url = "http://fleeceandthankyou.org/";
$html = file_get_html($url);
$value = $html->find('span.givecamp-header-wide', 0);
//If it can't find the element, throw an error
try
{
echo $value->innertext;
}
catch (Exception $e)
{
echo "Couldn't access magic method: " . $e->getMessage();
}
I am very new to both php and xml. What I am trying to do in
php is read in xml from a call to a url, and then parse the xml.
(I can get this to work in the example below when $urlip = 'localfile.xml'
but not when I put in a url. Ive checked the url by going to it with my browser,
and I can see the xml. I also did a show source, copied it and then pasted the
xml into the localfile and that works fine.
What am I doing wrong in trying to get the xml from the url?
Thank you
The error being returned is:
Error loading XML Start tag expected, ‘<' not found
Here is my code snip it:
$urlip="test.xml";# for debugging since I cannot read from the url yet! not sure why....
if (($xml = file_get_contents($urlip))===false) {
echo "error fetching XML\n";
} else {
libxml_use_internal_errors(true);
$data = simplexml_load_string($xml,null,LIBXML_NOCDATA);
if (!$data) {
echo "Error loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
} else {
foreach ($data as $item) {
$type = $item->TAB_TYPE;
$number=$item->ALT_ID;
$title = $item->SHORT_DESCR;
$searchlink = $item->ID;
$rsite=$item->CATEGORY;
echo "type $type, number $number, title $title, search link $searchlink, site $rsite\n";
}
}
}
Most likely situation from what it looks like:
Your function queries the remote URL and returns you an empty string, which passes the condition of your 'if' statement.
After that - you try to pass the empty string into XML, but it cannot, so it gives you an error.
Your steps to solve it:
configure php to open remote urls as comments to your question state - url_fopen
use another way to get content from the URL - cURL library works well
I'm working with an API which is returning some XML data that I'm unable to obtain.
When a login has been successful the session key becomes authenticated and the API returns the the boolean value 'true'.
This is formatted as follows:
This XML file does not appear to have any style information associated with it.
document tree is shown below.
<boolean xmlns="http://tessiturasoftware.com/">true</boolean>
Where other XML data has been formatted with more of a nested structure I've been able to use the following PHP to extract the data
$prodresponse = curl_exec($getproductions);
if(curl_errno($getproductions))
{
echo 'Curl error: Unable to obtain session ID ' . curl_error($getproductions);
}
else{
$xmlContent = simplexml_load_string($prodresponse);
echo $prodresponse;
foreach($xmlContent->xpath('//Production')as$prod){
$prodid= $prod->prod_season_no;
echo "<form action='GetProductionDetail.php' method='GET'>";
echo "<h1>".$prod->prod_desc." </h1> <input type='submit' value='Book Now'>
<input type='hidden' name='prodid' value=$prodid /></form>";
}
}
However when I attempt to use the following to return the boolean value and move to the account details page, no data is returned
$response2 = curl_exec($login);
$xmlContent = simplexml_load_string($response2);
if(curl_errno($login))
{
echo 'Curl error: Unable to login ' . curl_error($login);
}
else {
echo 'test';
foreach($xmlContent->xpath('boolean') as $bool) {
echo $bool;
echo 'test';
}
if ($bool=='true'){
echo'test';
header("Location: GetAccountDetails.php");
}
}
Please could someone tell me if I'm doing something wrong, whether it's to do with the simplexml_load_string or whether it would help to create a simpleXMLObject etc...?
Thanks
Caspian
The XML has a namespace definition. So the "real/internal" name of the element is {http://tessiturasoftware.com/}:boolean. To fetch this element without ignoring the namespace, you need to register a prefix for it. SimpleXML has the method registerXpathNamespace() for that. After that you can use the prefix as an alias for the namespace string. If you register tess the element can be addressed as tess:boolean.
I prefer DOM because it allows me to do more complex Xpath, like casting the result to string and compare it.
$dom = new DOMDocument();
$dom->loadXml('<boolean xmlns="http://tessiturasoftware.com/">true</boolean>');
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('tess', 'http://tessiturasoftware.com/');
var_dump(
$xpath->evaluate('string(/tess:boolean) = "true"')
);
Program Output
bool(true)
int number= getResources().getInteger(R.integer.yourNumber);
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).
The problem is only happening with one file when I try to do a DocumentDOM/SimpleXML method, so it seems like the issue is with that file. No clue what it could be.
If I do the following:
$file = "test1.html";
$dom = DOMDocument::loadHTMLFile($file);
$xml = simplexml_import_dom($dom);
print_r($xml);
in Chrome, I get a "Page Unavailable" error. In Firefox, I get nothing.
If I do the same thing but to a "test2.html", I get a print out as expected.
If I try the same thing but doing it this way:
$file = "test1.html";
$data = file_get_contents($file)
$dom = DOMDocument::loadHTML($data);
$xml = simplexml_import_dom($dom);
print_r($xml);
I get the same issue.
If I comment out the print_r line, Chrome goes from the "Page Unavailable" to blank.
I changed the permissions to 777, in case that was an issue, no fix.
I tried simply echoing out the contents of the html, no problem at all.
Any clues as to why a) Chrome would do that, and b) why I'm not getting any usable results?
Update:
If I put in:
$file = "test1.html";
$dom = DOMDocument::loadHTMLFile($file);
if(!$dom) {
echo "No Load!";
}
else {
$xml = simplexml_import_dom($dom);
print_r($xml);
}
I get the same issue. If I put in:
$file = "test1.html";
$dom = DOMDocument::loadHTMLFile($file);
if(!$dom) {
echo "No Load!";
}
else {
echo "Load!";
}
I get the "Load!" output, meaning that the dom method shouldn't be the problem (?)
I'll try the same exact test with the simplexml.
Update2:
If I do this:
I get the same issue. If I put in:
$file = "test1.html";
$dom = DOMDocument::loadHTMLFile($file);
$xml = simplexml_import_dom($dom);
if(!$xml) {
echo "No Load!";
}
else {
echo "Load!";
}
I get "Load!" but if I do:
$file = "test1.html";
$dom = DOMDocument::loadHTMLFile($file);
$xml = simplexml_import_dom($dom);
if(!$xml) {
echo "No Load!";
}
else {
echo "Load!";
print_r($xml);
}
I get the error. I did finally notice that I had an option to view the error in Chrome:
Error 324 (net::ERR_EMPTY_RESPONSE): Unknown error.
The troublesome html file is 288Kb. Could that be the issue? If so, how would I adjust for that?
Last Update:
Very Odd. I can use methods and functions on the object (as simplexml or domdocument), so I can do things like xpath to delete or parse the html, etc. In some cases (small results) it can echo out results, but for big stuff (show all spans), it fails in the same way.
So, since the end result, I think will fit in these parameters, I SHOULD be okay (I guess).
But any real solution is very welcome.
Turn on error reporting: error_reporting(E_ALL); in the first line of your PHP code.
Check the memory limit of your PHP configuration: memory_limit in the respective php.ini
What's the difference between test1.html and test2.html? Perhaps test1.html is not well-formed.
DocumentDOM and/or SimpleXML may bail out if the document is malformed. Try something like:
$dom = DOMDocument::loadHTMLFile($file);
if (!$dom) {
echo 'Loading file failed';
exit;
}
$xml = simplexml_import_dom($dom);
if (!$xml) {
...
}
If creating the $dom worked, conversion to $xml should work as well, but make sure anyway.
Edit: As Gehrig said, make sure error reporting is on, that should make it obvious where the process fails.