How to call XML data in PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
A simple program to CRUD node and node values of xml file
I tried calling the data in my XML in the below way. But it didn't work for me. Can anybody tell me how can I make it work?
<broad>
<site>
<title>My Site Name</title>
<caption>My Site Caption</caption>
<hostname>www.mydomain.com</hostname>
</site>
<broad>
My PHP file is
<?php
$xml = simplexml_load_file('settings.xml');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $xml->title; .'|'. $xml->caption; ?></title>
</head>
<body>
</body>
</html>
Why the above way is not working? is there any other way which is more easier than this?

You need to access the site node:
<?php echo (string) $xml->site->title?>
If ever in doubt, use a var_dump:
<?php echo '<pre>'; var_dump($xml); echo '</pre>'; ?>

Use the following:
<?php echo (string)$xml->site->title .'|'. (string)$xml->site->caption; ?>

Use SimpleXML debug with var_dump() and print_r()

Related

Get content from distant URL and test

I have to get data from a .html file, into a distant server. With file_gets_content I can retrieve the informations but when I want to test it I have some problems.
For example I can have 0 or 1 into my .html page. In my .php I want to do something if the file_gets_content return 0 or 1 but for now I didn't find how I can do it
Here is my .html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
0
</body>
</html>
My PHP code :
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');
You can use this php library https://github.com/sunra/php-simple-html-dom-parser
$dom =HtmlDomParser::file_get_html('http://192.168.1.XXX/wordpress/read.html');
$bodyText=$dom->find("body",0)->innertext;
alternative solution is
$home = file_get_contents('http://192.168.1.XXX/wordpress/read.html');
$dom = new DOMDocument();
$dom->loadHTML($home);
if(($body=$dom->getElementsByTagName("body"))->length>0){
$text=$body[0]->nodeValue
}

PHP DOMDocument double DOCTYPE

I got an situation with PHP DOMdocument.
I got a HTML file with an double doctype and i think therefor php can't reach the <head> element with $doc->getElementsByTagName('head'); (Length is returning 0).
So how can i remove the first DOCTYPE within the DOCdocument.
Here is the HTML file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
</head>
<body>
<Some html code>
</body>
</html>
To answer your QUESTION
You can do this by using array_slice() to remove the first line
e.g
$html = file_get_contents('index.html');
$html = implode("\n", array_slice(explode("\n", $html), 1));
To clarify your problem
The <head> tag IS empty.
To retrieve the contents within head
preg_match('/<head>(.*)<\/head>/s', $html, $matches);
var_dump($matches);

Generate div automatically by mysql_fetch_array

I would like to create a system to issue opinions on a given thing. I'm at a good point, but I can not get created for each element taken from the database a correspond div. Let me explain, for example if I have three reviews made by the three authors, I would like three divs for the reviews, and three for the authors, then automatically generated by the loop. How can i do?
You can do this.. First of all add <body> tag to your html document
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$Content = mysql_query("SELECT * FROM feedback");
while($Row = mysql_fetch_array($Content)) {
$Feedback = $Row['FeedbackUser'];
$UserNick = $Row['UserNickname'];
$UserMail = $Row['UserEmail'];
echo "<div>{$Feedback}</div>"; // Displaying feedback as div
echo "<div>{$UserNick }</div>"; // Displaying UserNick as div
echo "<div>{$UserMail }</div>"; // Displaying UserMail as div
}
?>
</body>
Hope this is what you are looking for

Using the PHP explode function with session variables [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to figure out what I'm doing wrong when I combine the explode function with session variables. I've done a decent amount of research already on session variables AND the explode function, but I've had trouble finding help on "combining" the two.
Below is the code I'm using. You'll notice that $_SESSION['shirt_type'] is being "exploded". This variable collects two pieces of info, the shirt type and price. I want to explode $_SESSION['shirt_type'] into two chunks.
However, when I try to echo chunk[0] or [1] using the following,
echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";
My web page echos the following
Shirt Type =
Shirt Price =
I believe I have the syntax wrong and I have not been able to find any place that demonstrates the correct syntax when trying to echo an exploded session variable.
Any help that can be offered will be GREATLY appreciated. Also, I've VERY new to StackOverflow so if I've posted something incorrectly, I apologize.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
session_start();
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);
$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>
<body>
<p>
<?php
session_start();
echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";
echo "Shirt Up Charge = {$_SESSION['$shirt_size_chunks[1]']}<br>";
echo "Shirt Size = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Qty = {$_SESSION['shirt_qty']}<br>";
echo "Price = {$_SESSION['price']}<br>";
?>
</p>
</body>
</html>
You should start session before any output.
Change your code to:
<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
<title>Untitled Document</title>
<?
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);
$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>
Second thing you have wrong here is that $_SESSION['shirt_type'] and $shirt_type can be the same variable if register_global = on on your system. Try to avoid using same names for variables as for global variables, such as $_POST['some_name'] and $some_name or $_SESSION['var_name'] and $var_name
And the third issue, noticed by #brenjt, you need only one call for session_start() in your script.
Fourth =) $_SESSION['$shirt_type_chunks[1]'] won't work. Using single quotes in PHP means that interpreter will not even try to find any variable in your string. Small example:
<?
$a = "Mike";
echo "Hi, my name is $a"; // will output Hi, my name is Mike
echo 'Hi, my name is $a'; // will output Hi, my name is $a
echo 'Hi, my name is '.$a; // will output Hi, my name is Mike
?>
Third way is a most efficient because in this case PHP sees single quotes and do not spend resources to analyse string. So change it to $_SESSION[$shirt_type_chunks[1]]
Here is what, as I suppose, you was heading to:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
$shirtDetails = explode("|", $_POST['shirt_type']);
$_SESSION['shirt_type'] = $shirtDetails['0'];
$_SESSION['shirt_price'] = $shirtDetails['1'];
$sizeDetails = explode("|", $_POST['shirt_size']);
$_SESSION['shirt_size'] = $sizeDetails[0];
$_SESSION['shirt_up_charge'] = $sizeDetails[1];
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];
$_SESSION['price'] = $_SESSION['shirt_qty'] * $_SESSION['shirt_price'] + $_SESSION['shirt_up_charge'];
?>
</head>
<body>
<p>
<?php
echo 'Shirt Type = '.$_SESSION['shirt_type'].'<br>';
echo 'Shirt Price = '.$_SESSION['shirt_price'].'<br>';
echo 'Shirt Up Charge = '.$_SESSION['shirt_up_charge'].'<br>';
echo 'Shirt Size = '.$_SESSION['shirt_size'].'<br>';
echo 'Shirt Qty = '.$_SESSION['shirt_qty'].'<br>';
echo 'Total Price = '.$_SESSION['price'].'<br>';
?>
</p>
</body>
</html>
One final question - why you not just create single input for each form property? Why you want this data to be concatenated by |, is it really necessary?

Simple RSS encoding issue

Consider the following PHP code for getting RSS news on a site I'm developing:
<?php
$url = "http://dariknews.bg/rss.php";
$xml = simplexml_load_file($url);
$feed_title = $xml->channel->title;
$feed_description = $xml->channel->description;
$feed_link = $xml->channel->link;
$item = $xml->channel->item;
function getTheData($item){
for ($i = 0; $i < 4; $i++) {
$article_title = $item[$i]->title;
$article_description = $item[$i]->description;
$article_link = $item[$i]->link;
echo "<p><h3>". $article_title. "</h3></p><small>".$article_description."</small><p>";
}
}
?>
The data accumulated by this function should be presented in the following HTML format:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Новини от Дарик</title>
</head>
<body>
<?php getTheData($item);?>
</body>
</html>
As you see I added windows-1251(cyrillic) and utf-8 encoding but the RSS feed is unreadable if I don't change the browser encoding to utf-8. The default encoding in my case is cyrilic but I get unreadable feed. Any help making this RSS readable in cyrilic(it's from Bulgaria) will be greatly appreciated.
I've just tested your code and the Bulgarian characters displayed fine when I removed the charset=windows-1251 meta tag and just left the UTF-8 one. Want to try that and see if it works?
Also, you might want to change your <html> tag to reflect the fact that your page is in Bulgarian like this: <html xmlns="http://www.w3.org/1999/xhtml" lang="bg" xml:lang="bg">
Or maybe you need to force the web server to send the content as UTF-8 by sending a Content-Type header:
<?php
header("Content-Type: text/html; charset=UTF-8");
?>
Just be sure to include this before ANY other content (even whitespace) is sent to the browser. If you don't you'll get the PHP "headers already sent" error.
Maybe you should take a look at htmlentities.
This can convert to html some characters.
$titleEncoded = htmlentities($article_title,ENT_XHTML,cp1251);

Categories