I was trying to follow the tutorials found on: https://www.ibm.com/developerworks/xml/library/x-phpwikipedia/index.html
All tutorials use Zend framework. I was wondering how this code can be adapted to avoid the use of Zend (or any other prerequisites) if possible? Please see example below. Thanks.
<?php
// load Zend classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Rest_Client');
// define category prefix
$prefix = 'hollywood';
try {
// initialize REST client
$wikipedia = new Zend_Rest_Client('http://en.wikipedia.org/w/api.php');
// set query parameters
$wikipedia->action('query');
$wikipedia->list('allcategories');
$wikipedia->acprefix($prefix);
$wikipedia->format('xml');
// perform request
// iterate over XML result set
$result = $wikipedia->get();
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
?>
<html>
<head></head>
<body>
<h2>Search results for categories starting with
'<?php echo $prefix; ?>'</h2>
<ol>
<?php foreach ($result->query->allcategories->c as $c): ?>
<li><a href="http://www.wikipedia.org/wiki/Category:
<?php echo $c; ?>"><?php echo $c; ?></a></li>
<?php endforeach; ?>
</ol>
</body>
</html>
Something like this should work:
<?php
$prefix = 'allcategories';
$url = "http://en.wikipedia.org/w/api.php?action=query&list={$prefix}&format=json&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
?>
<html>
<head></head>
<body>
<h2>Search results for categories starting with
'<?php echo $prefix; ?>'</h2>
<ol>
<?php foreach ($data->query->allcategories as $c):
?>
<li><?php echo $c->{'*'}; ?></li>
<?php endforeach; ?>
</ol>
</body>
</html>
In this case request will be using json format, its easier to live with :)
As the IBM article mentions, the Wiki API by default returns results in XML but can also return results in JSON, WDDX, YAML, or PHP serialized.
Basically, you just need to make an HTTP request that requests the correct information and parses the returned results. For example click this link http://en.wikipedia.org/w/api.php?action=query&list=allcategories&acprop=size%20&acprefix=hollywood&format=xml and you see it returns XML results in the browser which you can pretty much use any language to loop over and parse.
To request the contents of the xml data in PHP you could use something file_get_contents($target_url); and then you wouldn't have to rely on creating a zend rest client.
Related
I'm trying to parse the site, but the cat doesn't output anything
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.text-part') as $element) {
echo $element->outertext;
}
?>
There are no Elements in the document which match the class .text-part. You can look at the source code when you save the HTML into a file.
<?php
include_once 'simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
file_put_contents('htmlData.html', $html);
When you try for example to find .block-top-section-posts you'll get a result.
<?php
include_once 'simplehtmldom_1_9_1/simple_html_dom.php';
$html = file_get_html('https://teleprogramma.pro/headlines');
foreach($html->find('.block-top-section-posts') as $element) {
echo $element->outertext;
}
// Outputs
/*
<div class="vue-container block-top-section-posts"> <div id="vuetag-top-section-posts" class="vue-tag news-line" data-url="/rest/term-additional-loading/section/76217/0/0" data-max-width="0" data-min-width="0" data-information=""> </div> </div>
*/
When you lookup the Site in a Browser you will get redirected to another URL. If you want to use that, have a look at php get url of redirect from source url to get the final address.
Im new to Php. I kinda need code to open Links in browser when script is loaded.
heres my code below.
<?php
$links = array_open("https://stackoverflow.com/",
"https://outlook.office.com/",
"https://www.protectedtext.com/",
"https://www.adobe.com/",
"https://www.linkedin.com");
echo $links[array_rand($links)];
?>
Hi change array_open to array
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
echo $links[array_rand($links)];
?>
And if you want to send the browser to the link you would send a location header like so...
<?php
$links = array("https://stackoverflow.com/", "https://outlook.office.com/", "https://www.protectedtext.com/", "https://www.adobe.com/", "https://www.linkedin.com");
header('Location: ' . $links[array_rand($links)]);
exit;
?>
Why does this if statement have each of its conditionals wrapped in PHP tags?
<?php if(!is_null($sel_subject)) { //subject selected? ?>
<h2><?php echo $sel_subject["menu_name"]; ?></h2>
<?php } elseif (!is_null($sel_page)) { //page selected? ?>
<h2><?php echo $sel_page["menu_name"]; ?></h2>
<?php } else { // nothing selected ?>
<h2>Select a subject or a page to edit</h2>
<?php } ?>
Because there is html used. Jumping between PHP and HTML is called escaping.
But I recommend you not to use PHP and HTML like this. May have a look to some template-systems e.g. Smarty or Frameworks with build-in template-systems like e.g. Symfony using twig.
Sometimes its ok if you have a file with much HTML and need to pass a PHP variable.
Sample
<?php $title="sample"; ?>
<html>
<title><?php echo $title; ?></title>
<body>
</body>
</html>
This is not much html but a sample how it could look like.
That sample you provided us should more look like....
<?php
if(!is_null($sel_subject))
{ //subject selected?
$content = $sel_subject["menu_name"];
}
else if (!is_null($sel_page))
{ //page selected?
$content = $sel_page["menu_name"];
}
else
{ // nothing selected
$content = "Select a subject or a page to edit";
}
echo "<h2>{$content}</h2>";
?>
You could echo each line of course. I prefer to store this in a variable so I can easy prevent the output by editing one line in the end and not each line where I have added a echo.
According to some comments i did a approvement to the source :)
Because the <h2> tags are not PHP and will display an error if the PHP Tags are removed.
This code will display one line of text wrapped in <h2> tags.
This is called escaping.
Because you cannot just type html between your php tags.
However, I would rather use the following syntax because it is easier to read. But that depends on the programmers opinion.
<?php
if(!is_null($sel_subject))
{ //subject selected?
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
}
elseif (!is_null($sel_page))
{ //page selected?
ehco "<h2>" . $sel_page["menu_name"] . "</h2>";
}
else
{ // nothing selected
echo "<h2>Select a subject or a page to edit</h2>";
}
Because inside the if-statement there is an HTML code, which you can put it by closing PHP tags and open it again like this:
<?php if(/*condition*/){ ?> <html></html> <?php } ?>
or:
<?php if(/*condition*/){ echo '<html></html>' ; }
That is because in this snippet we see html and php code. The code <?php changes from html-mode to php-mode and the code ?> changes back to html-mode.
There are several possibilites to rewrite this code to make it more readable. I'd suggest the following:
<?php
//subject selected?
if (!is_null($sel_subject)) {
echo "<h2>" . $sel_subject["menu_name"] . "</h2>";
//page selected?
} elseif (!is_null($sel_page)) {
echo "<h2>" . $sel_page["menu_name"] . "</h2>";
// nothing selected
} else {
echo "<h2>Select a subject or a page to edit</h2>";
}
?>
using the echo-command to output html, you don't need to change from php-mode to html-mode and you can reduce the php-tag down to only one.
Using MVC I have a model directory inside of which are two files; menu.xml and the following model.php.
<?php
$xml = simplexml_load_file('menu.xml');
print_r ($xml);
?>
In my views directory I have an index.php files as follows:
<?php
include '../model/model.php';
print_r($xml);
foreach ($xml->children() as $option)
{
?>
<li><a href='<?= $option->getName() ?>'><?= $option['name'] ?></li>
<?php
}
?>
</ul>
In another directory - this time html - i have the following:
<!-- This is the controller -->
<?php
require_once('../includes/helpers.php');
// determine which page to render
if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = 'index';
// show page
switch ($page)
{
case 'index';
render('templates/header', array('title' => 'Main Menu'));
render('index');
render('templates/footer');
break;
}
?>
This is supposed to show a list of the children of the menu.xml file. If I access model.php directly:
<pre>
<?php
$xml = simplexml_load_file('menu.xml');
print_r ($xml);
?>
</pre>
I can see the contents of the $xml object.
So why doesn't the include '../model/model.php command work?
Any tips appreciated!
Thanks
$xml = simplexml_load_file('menu.xml');
This path is relative to the current executed script. That’s why it works when your accessing model/model.php directly, but not when including it from ./index.php. Use absolute path instead:
$xml = simplexml_load_file(__DIR__ . '/menu.xml');
Edit by IMSoP:
For PHP versions older than 5.3:
$xml = simplexml_load_file(dirname(__FILE__) . '/menu.xml');
I'm making RSS feed reader with PHP and i get all feed content, but i can't figure out hot to target RSS values so i can echo them, style them, etc!
this is the feed
my script so far:
foreach($feed_list as $from => $feeds) {
$xml = simplexml_load_file($feeds);
echo "<pre>";
print_r($xml);
echo "</pre>";
}
I get this out after code executes
Sorry for trouble, but i found out how
<?php
$rss = simplexml_load_file('http://www.riga.lv/rss/lv/PressRelease/');
?>
<h1><?php echo $rss->channel->title; ?></h1>
<ul>
<?php
foreach($rss->channel->item as $e) {
echo "<li><a href=\"".$e->link['href']."\">";
echo $e->title;
echo "</a></li>\n";
}
?>
Thanks to this :) - Thanks Stack Overflow :)
You need to traverse the $xml object returned and get the values for each single node... check http://www.php.net/manual/en/class.simplexmlelement.php for more info.