Hi i need help to get all categories from rss.
This is ex. of my rss feed:
<item>
<title>Coca-Cola</title>
<link>https://www.tralaaa.com/coca-cola/</link>
<comments></comments>
<pubDate>Fri, 01 Dec 2017 11:36:40 +0000</pubDate>
<dc:creator><![CDATA[Admin]]></dc:creator>
<category><![CDATA[cat1]]></category>
<category><![CDATA[cat2]]></category>
<category><![CDATA[cat3]]></category>
<category><![CDATA[cat4]]></category>
<description><![CDATA[]]></description>
<content:encoded><![CDATA[]]></content:encoded>
</item>
I try with $cat = $item->category(0); but give me the error
Call to undefined method SimpleXMLElement::category()
You cant access category using () parenthesis, you need to use [], like this:
$xml = new SimpleXMLElement(
'<item>
<title>Coca-Cola</title>
<link>https://www.tralaaa.com/coca-cola/</link>
<comments></comments>
<pubDate>Fri, 01 Dec 2017 11:36:40 +0000</pubDate>
<dc:creator><![CDATA[Admin]]></dc:creator>
<category><![CDATA[cat1]]></category>
<category><![CDATA[cat2]]></category>
<category><![CDATA[cat3]]></category>
<category><![CDATA[cat4]]></category>
<description><![CDATA[]]></description>
<content:encoded><![CDATA[]]></content:encoded>
</item>');
$cat_name1 = $xml->category[0]->__toString();
$cat_name2 = $xml->category[1]->__toString();
$cat_name3 = $xml->category[2]->__toString();
$cat_name4 = $xml->category[3]->__toString();
echo "<pre>";
print_r($cat_name1);
echo "</pre>";
echo "<pre>";
print_r($cat_name2);
echo "</pre>";
echo "<pre>";
print_r($cat_name3);
echo "</pre>";
echo "<pre>";
print_r($cat_name4);
echo "</pre>";
this will output:
you can just:
$xml->category->__toString();
it will assume you want the first category.
Related
There are several tags in the xml file. How can I find an tag with the content in "BRL"?
i have tried
$usd_brazilRate = $usdXML->item[89]->title;<br>
$usd_brazilDate = $usdXML->item[89]->pubDate;<br>
but the item number (position) always changes
example cropped xml content:
<channel>
<item>
<title>1 USD = 64.78833120 RUB</title>
<link>http://www.floatrates.com/usd/rub/</link>
<description>1 U.S. Dollar = 64.78833120 Russian Rouble</description>
<pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>RUB</targetCurrency>
<targetName>Russian Rouble</targetName>
<exchangeRate>64.78833120</exchangeRate>
<inverseRate>0.01543488</inverseRate>
<inverseDescription>1 Russian Rouble = 0.01543488 U.S. Dollar</inverseDescription>
</item>
<item>
<title>1 USD = 3.92245587 BRL</title>
<link>http://www.floatrates.com/usd/brl/</link>
<description>1 U.S. Dollar = 3.92245587 Brazilian Real</description>
<pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>BRL</targetCurrency>
<targetName>Brazilian Real</targetName>
<exchangeRate>3.92245587</exchangeRate>
<inverseRate>0.25494232</inverseRate>
<inverseDescription>1 Brazilian Real = 0.25494232 U.S. Dollar</inverseDescription>
</item>
<item>
<title>1 USD = 0.76733706 GIP</title>
<link>http://www.floatrates.com/usd/gip/</link>
<description>1 U.S. Dollar = 0.76733706 Gibraltar pound</description>
<pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>GIP</targetCurrency>
<targetName>Gibraltar pound</targetName>
<exchangeRate>0.76733706</exchangeRate>
<inverseRate>1.30320826</inverseRate>
<inverseDescription>1 Gibraltar pound = 1.30320826 U.S. Dollar</inverseDescription>
</item>
</channel>
$usdXML = simplexml_load_file("http://www.floatrates.com/daily/usd.xml") or die("Failed to load");
$usd_brazilRate = $usdXML->item->title;
$usd_brazilDate = $usdXML->item->pubDate;
output
1 USD = 3.92245587 BRL
Thu, 2 May 2019 12:00:02 GMT
You can use XPath:
$brazil = $usdXML->xpath('/channel/item[targetCurrency="BRL"]');
print($brazil[0]->description . "\n");
print($brazil[0]->pubDate . "\n");
This is my code:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$title=$html->find('div#ires', 0)->innertext;
echo $title;
?>
It outputs all result of the Google Search Page under the Search "BA236".
The problem is I dont need all of them and the Information I need is inside a div that has no id or class or anything else.
The div I need is inside the first
<div class="g">
on the Page, so maybe I should try something like this:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$title=$html->find('div[class=g], 0')->innertext;
echo $title;
?>
But the Problem of that is, if I load the page it shows me nothing except this:
Notice: Trying to get property of non-object in
C:\xampp\htdocs...\simpletest2.php on line 4
So how can i get the div i´m searching for and what am I doing wrong ?
Edit:
Solution:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$e = $html->find("div[class=g]");
echo $e[0]->innertext;
?>
Or:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$title=$html->find('div[class=g]')[0]->innertext;
echo $title;
?>
I made a change to your code where I am searching for the class:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$e = $html->find("div[class=g]");
echo $e[0]->innertext;
?>
result:
British Airways Flight 236
Scheduled departs in 13 hours 13 mins
Departure DME 5:40 AM —
Moscow Dec 15
Arrival LHR 6:55 AM Terminal 5
London Dec 15
Scheduled departs in 1 day 13 hours
Departure DME 5:40 AM —
Moscow Dec 16
Arrival LHR 6:55 AM Terminal 5
London Dec 16
I looked for the div elements with class g then I printed the count of the first element '0'
$e = $html-> find ("div [class = g]");
echo $e [0]->innertext;
Your code:
<?php
include('simple_html_dom.php');
$html = file_get_html('http://www.google.com/search?q=BA236',false);
$title=$html->find('div[class=g]')[0]->innertext;
echo $title;
?>
not ('div[class=g], 0')
but ('div[class=g]')[0]
there is no need for simple_html_dom here, it's easy to do with the builtins DOMDocument and DOMXPath.
<?php
$html = file_get_contents('http://www.google.com/search?q=BA236');
echo (new DOMXPath ( (#DOMDocument::loadHTML ( $html )) ))->query ( '//div[#class="g"]' )->item ( 0 )->textContent;
in my opinion, DOMDocument + DOMXPath makes simple_html_dom.php rather pointless. the former 2 can do pretty much everything simple_html_dom can do, and are built-in native php functions, which is likely to be maintained as long as PHP itself is maintained, and the latter is a 3rd party project which seems nearly dead by the looks of it (last commit was in 2014, there was only 1 commit in all of 2014, and 2 commits in all of 2013 )
The XML file I want to read looks like this:
<issues aaa="444" exportTime="Tue Jul 28 23:54:39 CEST 2015">
<issue>
<name>Testing</name>
I use this code:
$simple = file_get_contents('/url/test3.xml');
$test = new SimpleXMLElement($simple);
I can read the issue name children:
echo $test->issue[1]->name;
How can I read issues ExportTime value?
echo $test->issues[exportTime]
doesn't do anything.
Try
<?php
$str = <<<XML
<issues aaa="444" exportTime="Tue Jul 28 23:54:39 CEST 2015">
<issue>
<name>Testing</name>
</issue>
</issues>
XML;
$xml = new SimpleXMLElement($str);
var_dump($xml['exportTime']);
?>
OUTPUT
object(SimpleXMLElement)[2]
public 0 => string 'Tue Jul 28 23:54:39 CEST 2015' (length=29)
I am trying to grab my twitter feed using the following code:
// Make the request and get the response into the $json variable
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
// It's json, so decode it into an array
$result = json_decode($json);
// Access the profile_image_url element in the array
echo $result->created_at;
?>
I get the result of:
Thu Oct 25 18:40:50 +0000 2012
If I try to get the text with:
echo $result->text;
I get this error:
Notice: Undefined property: stdClass::$text in /Library/WebServer/Documents/include/twitter_noformat/items.php on line 35
A partial var_dump of my data format includes this:
{"created_at":"Thu Aug 01 16:12:18 +0000 2013",
"id":362969042497175553,
"id_str":"362969042497175553",
"text":"A warm welcome to our new international students from China, Hong Kong and Germany! http:\/\/t.co\/GLvt3GynJV",
"source":"web",
"truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":
My question is:
created_at gives me a value. id gives me a value. Why doesn't text? I know nothing about JSON btw. I'm not a very advanced programmer, but the pattern looks the same to me.
Edit: Well I found a cool snippet that converted my twitter array to something more readable. The function goes like this:
// It's json, so decode it into an array
$result = json_decode($json);
// Access the profile_image_url element in the array
$pretty = function($v='',$c=" ",$in=-1,$k=null)use(&$pretty){$r='';if(in_array(gettype($v),array('object','array'))){$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").'<br>';foreach($v as $sk=>$vl){$r.=$pretty($vl,$c,$in+1,$sk).'<br>';}}else{$r.=($in!=-1?str_repeat($c,$in):'').(is_null($k)?'':"$k: ").(is_null($v)?'<NULL>':"<strong>$v</strong>");}return$r;};
echo $pretty($result);
The results now look like this:
statuses_count: 583
lang: en
status:
created_at: Thu Aug 01 21:10:10 +0000 2013
id: 363044004444651522
id_str: 363044004444651522
text: #CalStateEastBay AD Sara Lillevand Judd '86 honored for her work as an athletic adminstrator. http://t.co/WzOqjIDrBw
This is strange because that makes text look like it's part of an object?
I have determined that twitter kicks back an array of objects. Those objects can have a lot of items(?) As I mentioned previously though I can echo $result->created_at; but not text. They are both at the same level of the array.
thanks in advance for your help,
Donovan
Alright here was my solution after a day of research:
$result = json_decode( $json );
echo "Text:" . $result->status->text . "<br />";
Text was a child(?) of status. I could echo created_at because it was used at two levels of the array, which I hadn't seen before. Text was inside the status object I guess.
created_at: Thu Oct 25 18:40:50 +0000 2012
favourites_count: 1
utc_offset: -25200
time_zone: Pacific Time (US & Canada)
geo_enabled: 1
verified:
statuses_count: 583
lang: en
status:
created_at: Thu Aug 01 21:10:10 +0000 2013
id: 363044004444651522
id_str: 363044004444651522
text: #CalStateEastBay AD Sara Lillevand Judd '86 honored for her work as an athletic adminstrator. http://t.co/WzOqjIDrBw
This is what I currently have for my PHP file:
<?php header("Content-type: text/xml"); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; ?>
<rss version="2.0">
<channel>
<title>My Website</title>
<link>http://www.mywebsite.com</link>
<description>The Title of My Website</description>
<pubDate>Tue, 15 Apr 2008 18:00:00 +0000</pubDate>
<item>
<title>Website Directory - Page NUMBER</title>
<pubDate><?echo date('Y/m/d H:i:s');?></pubDate>
<link>http://www.mywebsite.com/directory/NUMBER</link>
<description>New update to page NUMBER in the Website Directory.</description>
</item>
</channel>
</rss>
That right there is correctly showing one entry in the RSS feed. However, I need it to show 30 entries, each with a random number entered in the three places where NUMBER is shown in the item.
Each RSS item should have a different number between 1 and 2779503 entered in the three places that say NUMBER. I know that PHP has http://php.net/manual/en/function.rand.php but what I don't know how to do is have it loop through 30 random numbers each time the feed is loaded...
<?php
foreach( range( 1, 30 ) as $i ):
$number = mt_rand( 1, 2779503 );
?>
<item>
<title>Website Directory - Page <?php echo $number; ?></title>
<pubDate><?echo date('Y/m/d H:i:s');?></pubDate>
<link>http://www.mywebsite.com/directory/<?php echo $number; ?></link>
<description>New update to page <?php echo $number; ?> in the Website Directory.</description>
</item>
<?php endforeach; ?>