SimpleXML in PHP Loop - php

im trying to loop through some XML data using PHP. Currently it just only brings back one, but i want it to bring back all the data.
<?php
$request_url = "http://finlay.tumblr.com/api/read";
$xml = simplexml_load_file($request_url);
$title = $xml->posts->post->{'regular-title'};
$post = $xml->posts->post->{'regular-body'};
$link = $xml->posts->post['url'];
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post.'</p>';
echo "...";
echo "</br><a target=frame2 href='".$link."'>Read More</a>";
?>

$request_url = "http://finlay.tumblr.com/api/read";
$xml = simplexml_load_file($request_url);
foreach($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$link = $post['url'];
$small_post = substr($post,0,320);
echo '<h1>'.$title.'</h1>';
echo '<p>'.$small_post.'</p>';
echo "...";
echo "</br><a target=frame2 href='".$link."'>Read More</a>";
}

Related

I want to Echo IMDB ID, Title, Quality and Embed URL From This JSON URL

Now I have this JSON URL https://vidsrc.me/movies/latest/page-1.json I want to echo IMDB ID, Title, Quality and Embed URL for each array as this sounds like a multidimensional array. But every time I try to do this. I get one of the following errors:
- Undefined Index
- Undefined offset
So I want to loop through it and echo each of those items and break line between each,
This is the code
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
$res = $decode->result;
foreach($decode as $value){
$imdb = $res->imdb_id;
$title = $res->title;
$quality = $res->quality;
$url = $res->embed_url;
echo $imdb;
echo "<br>";
echo $tite;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>
json_decode() returns you array but you process it like object, you should process it as array:
<?php
$url = file_get_contents('https://vidsrc.me/movies/latest/page-1.json');
$decode = json_decode($url, TRUE);
foreach($decode['result'] as $value){
$imdb = $value['imdb_id'];
$title = $value['title'];
$quality = $value['quality'];
$url = $value['embed_url'];
echo $imdb;
echo "<br>";
echo $title;
echo "<br>";
echo $quality;
echo "<br>";
echo $url;
}
?>

simple_html_dom Cannot add node in second level

I cannot find my second level node, the object is empty
include_once("simple_html_dom.php");
$simple = new simple_html_dom();
$simple->load("<div id='base'>divbase</div>");
$base = $simple->find("#base",0);
echo $simple->outertext."<br>";
echo "base=".$base->innertext."<br>";
$base->innertext .= "<div id='div_1_'>div1</div>";
$ch = $simple->save();
echo $ch."<br>";
$trouv = $simple->find('#div_1_',0);
$trouv->innertext .= "<div id='div_1_0_'>some text</div>";
$ch = $simple->save();
echo $ch."<br>";
the var $trouv is empty why?
I think that is because you add <div id='div_1_0_'>some text</div> to the innertext but that is not being parsed as html.
What you might do is load the modified html again using $simple->load($simple->save());
You code could look like:
$simple = new simple_html_dom();
$simple->load("<div id='base'>divbase</div>");
$base = $simple->find("#base",0);
echo $simple->outertext."<br>";
echo "base=".$base->innertext."<br>";
$base->innertext .= "<div id='div_1_'>div1</div>";
$simple->load($simple->save());
$ch = $simple->save();
echo $ch."<br>";
$trouv = $simple->find('#div_1_',0);
$trouv->innertext .= "<div id='div_1_0_'>some text</div>";
$ch = $simple->save();
echo $ch."<br>";

PHP foreach with two 'as'?

Hi there i am trying to combine two loops of foreach but i have a problem.
The problem is that the <a href='$link'> is same to all results but they must be different.
Here is the code that i am using:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
$doc = new DOMDocument();
#$doc->loadHTML($feed);
$tags = $doc->getElementsByTagName('link');
foreach ($tags as $tag) {
foreach($rss as $r){
$title = $r->title;
$content = $r->content;
$link = $tag->getAttribute('href');
echo "<a href='$link'>$title</a> <br> $content";
}
}
?>
Where i my mistake? Why it's not working and how i make it work properly?
Thanks in advance!
Both loops were going through different resources so you are just simply cross joining all records in them.
This should work to get the data you need:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
foreach ($rss as $key => $entry) {
if ($key == "entry")
{
$title = (string) $entry->title;
$content = (string) $entry->content;
$link = (string) $entry->link["href"];
echo "<a href='$link'>$title</a><br />" . $content;
}
}

foreach in foreach only echos last item?

I am trying to extract download links from a site. However i only get the last item in my array.
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
foreach ($dllinks as $value)
{
echo $value . '<br>';
}
?>
When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.
EDIT:
Sorry it was supposed to be like this
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('td.tlistdownload a') as $links){
$dllinks[] = $links->href;
}
foreach($html->find('td.tlistname a') as $names){
echo '<a href="';
foreach ($dllinks as $value)
{
echo $value;
}
echo '">' . $names->innertext . '</a><br>';
}
?>
I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..
<?php
require 'functions/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
$page_title = $html->find('title',0);
?>
Title:<?php echo $page_title->plaintext; ?><br><br>
Links:<br>
<?php
foreach($html->find('.tlistrow') as $row){
$link_nodes = $row->find('td.tlistdownload a');
$name_nodes = $row->find('td.tlistname a');
if (count($link_nodes) > 0 && count($name_nodes) > 0) {
$link = $link_nodes[0]->href;
$name = htmlentities($name_nodes[0]->innertext);
echo "<a href='{$link}'>{$name}</a>\n";
}
}

echo xml with tags on html

I am trying to echo xml and I cant mange to echo the xml tags
,this is my code
<?php
// set feed URL
$feedURL = 'http://gdata.youtube.com/feeds/api/videos?orderby=updated&vq=rihana';
// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<h1><?php echo $sxml->title; ?></h1>
<?php
$xml = new SimpleXMLElement('<xml></xml>');
// iterate over entries in feed
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$watch = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$thumbnail = $attrs['url'];
$track = $xml->addChild('item');
$track->addChild('description', $media->group->description);
$track->addChild('title', $media->group->title);
$track->addChild('url', $watch);
}
?>
<p>
<?php
header('Content-type: text/xml');
echo( htmlentities( $xml->asXML() ) );
foreach ($xml->item as $record) {
echo '<div class="item_record">'."\n";
echo '<h3>'.$record->title.'</h3>'."\n";
echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";
echo '</div>'."\n";
}?>
</p>
i really want to print the data in xml format its like a mix from the code below
echo( htmlentities( $xml->asXML() ) );
and look nice like
foreach ($xml->item as $record) {
echo '<div class="item_record">'."\n";
echo '<h3>'.$record->title.'</h3>'."\n";
echo '<p><span class="category">description: </span>'.$record->description.'</p>'."\n";
echo '<p><span class="category">url: </span><a href='.$record->url.'>'.$record->url.'</a></p>'."\n";
echo '</div>'."\n";
}?>
which operation is the best to solve my problem?
This may help you :
$dom = new DOMDocument("1.0");
$parent = $dom->appendChild($dom->createElement("PARENT1"));
$parent->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
$child = $root->appendChild($dom->createElement("CHILD1"));
$child->appendChild($dom->createTextNode('VALUE_FOR_CHILD1'));
$dom->encoding = "UTF-8";
$xml = $dom->saveXML();
echo $xml;
Will give you something like this :
<?xml version="1.0" encoding="UTF-8"?>
<PARENT1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CHILD1>VALUE_FOR_CHILD1</CHILD1>
</PARENT1>

Categories