Pulling data from Google Cal xml - php

I'm running into an error when trying to pull some data from Google Cal using PHP. Here is the code:
$url = $_POST['url'];
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$ns= $feed->getNameSpaces(true);
foreach ($feed->entry as $entry) {
$eventdetail = $entry->children($ns["gd"]);
$when_atr = $eventdetail->when[0]->attributes();
$title = $entry->title;
echo "<div class='eventTitle'>".$title . "</div>";
$where = $eventdetail->where[0]->attributes();
echo "<div class='eventLocation'>" . $where . "</div>" . '<br />';
$start = new DateTime($when_atr['startTime']);
echo "<div class='eventTime'>".$start->format('D F jS, g:ia') . " to ";
$end = new DateTime($when_atr['endTime']);
echo $end->format('g:ia')."</div>" . '<br />' ;
}
The titles work just fine, but for both the "when" and "where" I'm running into problems with the code when[0]->attributes(); and where[0]->attributes();.
The error I get is
Fatal error: Call to a member function attributes() on a non-object
What can I do to prevent this from happening?

Related

Php Xpath query returning "Array"

ive got my xml file and also heres my php script
$db = simplexml_load_file("BIN/videos.xml");
$id = $_GET['id'];
$tq = "//video['#id=" . $id . "']/title[0]";
$dq = "//video['#id=" . $id . "']/description[1]";
$eq = "//video['#id=" . $id . "']/embed[2]";
$title = $db->xpath($tq);
$description = $db->xpath($dq);
$embed = $db->xpath($eq);
include("design/lyt.php");
echo $embed . '<br>
<h1>' . $title . '</h1>
<p>' . $description . '</p>';
?>
Its supposed to display "Test" for them all! but it says "Array"
Access the elements of the array returned from xpath...
PHP >= 5.4:
$title = $db->xpath($tq)[0];
PHP < 5.4:
Update PHP :-)
or
list($title,) = $db->xpath($tq);
or
$title = $db->xpath($tq);
$title = $title[0];

simplexml 'Trying to get property of non-object in' error

I'm trying to parse the XML of http://www.mpgh.net/forum/external.php?type=RSS2&forumids=175
but I get this error, can't find whats wrong.
Notice: Trying to get property of non-object in C:\xampp\htdocs\crossfire\index.php on line 9
<?php
$rss = simplexml_load_file('http://www.mpgh.net/forum/external.php?type=RSS2&forumids=175');
for($i=0;$i<10;$i+=1) {
$namespaces = $rss->getNameSpaces(true);
$dc = $rss->children($namespaces['dc']);
echo "Title: " . $rss->channel->item[$i]->title . "<br>";
echo "Creator: " . $dc->channel->item[$i]->creator . "<br>";
echo "Link: " . $rss->channel->item[$i]->link . "<br><br>";
}
And my second question.
Why is this code only working properly at http://www.mpgh.net/forum/external.php?type=RSS2&forumids=175 and not at other pages like http://www.mpgh.net/forum/external.php?type=RSS2&forumids=168
Notice: Trying to get property of non-object in C:\xampp\htdocs\crossfire\index.php on line 7
<?php
$rss = New DOMDocument();
$rss = simplexml_load_file('http://www.mpgh.net/forum/external.php?type=RSS2&forumids=168');
for($i=0;$i<10;$i+=1) {
if (substr($rss->channel->item[$i]->title, 0, 9) == '[Release]') {
echo "Title: " . $rss->channel->item[$i]->title . "<br>";
echo "Link: " . $rss->channel->item[$i]->link . "<br><br>";
} else {
echo 'Hoi<br><br>';
}
}
Thanks.
I think the problem is that $ith index of $rss->channel->item is not set or not an object.
Try this; it will expose the problem:
$rss = simplexml_load_file('http://www.mpgh.net/forum/external.php?type=RSS2&forumids=175');
if ($rss===null || !is_object($rss))
die('Failed to load xml file.');
if (!is_object($rss->channel))
die('Channel is not an object!');
foreach ($rss->channel->item as $item)
if (is_object($item)) {
$namespaces = $rss->getNameSpaces(true);
$dc = $rss->children($namespaces['dc']);
echo "Title: " . $item->title . "<br>";
echo "Creator: " . $item->creator . "<br>";
echo "Link: " . $item->link . "<br><br>";
}

Trying to read values from an XML feed and display them in PHP

I'm trying to display values from this xml feed:
http://www.scorespro.com/rss/live-soccer.xml
In my PHP code I have the following loop but it does not display the results on my page:
<?php
$xml = simplexml_load_file("http://www.scorespro.com/rss/live-soccer.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $item)
{
echo $item->getName() . ": " . $item->name . "<br>";
}
?>
For some reason it only shows:
rss
channel:
I'm fairly new to how XML works so any help would be much appreciated.
You can get actual data from $xml->channel->item so use like below
$items = $xml->channel->item;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
$pubDate = $item->pubDate;
$description = $item->description;
}
DEMO.
you can use below code
$dom = new DOMDocument;
$dom->loadXML($url);
if (!$dom) {
echo 'Error while parsing the document';
exit;
}
$xml = simplexml_import_dom($dom);
$data = $xml->channel->item;

Call to a member function find() on a non-object simpleHTMLDOM

I am trying to read a link from one page, print the URL, go to that page, and read the link on the next page in the same location, print the url, go to that page (and so on...).
All I'm doing is reading the URL and passing it as an argument to the get_links() function until there are no more links.
This is my code but it throws:
Fatal error: Call to a member function find() on a non-object.
Anyone know how to fix this?
<?php
$mainPage = 'https://www.bu.edu/link/bin/uiscgi_studentlink.pl/1346752597?ModuleName=univschr.pl&SearchOptionDesc=Class+Subject&SearchOptionCd=C&KeySem=20133&ViewSem=Fall+2012&Subject=&MtgDay=&MtgTime=';
get_links($mainPage);
function get_links($url) {
$data = new simple_html_dom();
$data = file_get_html($url);
$nodes = $data->find("input[type=hidden]");
$fURL = $data->find("/html/body/form");
$firstPart = $fURL[0]->action . '<br>';
foreach ($nodes as $node) {
$val = $node->value;
$name = $node->name;
$name . '<br />';
$val . "<br />";
$str1 = $str1 . "&" . $name . "=" . $val;
}
$fixStr1 = str_replace('&College', '?College', $str1);
$fixStr2 = str_replace('Fall 2012', 'Fall+2012', $fixStr1);
$fixStr3 = str_replace('Class Subject', 'Class+Subject', $fixStr2);
$fixStr4 = $firstPart . $fixStr3;
echo $nextPageURL = chop($fixStr4);
get_links($nextPageURL);
}
?>
Alright so I was using the load->file() function somewhere in my code and did not see it until I really scraped through it. Finally have a running script :) The key is to use file_get_html instead of loading the webpage as an object using the load->file() function.

parsing a xml to get some values

http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0
These are player sales from a browser game. I want to save some fields from these sales. I am fetching that xml with curl and storing on my server. Then do the following:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('*/transfer');
print_r($items);
foreach($items as $item) {
echo $item['buyerTeamname'], ': ', $item['sellerTeamname'], "\n";
}
The array is empty and i cant seem to get anything from it. What am i doing wrong?
There is no reason to use cURL or XPath for that. You can do
$url = 'http://www.managerleague.com/export_data.pl?data=transfers&output=xml&hide_header=0';
$transfers = new SimpleXMLElement($url, NULL, TRUE);
foreach($transfers->transfer as $transfer) {
printf(
"%s transfered from %s to %s\n",
$transfer->playerName,
$transfer->sellerTeamname,
$transfer->buyerTeamname
);
}
Live Demo
You forgot a slash in your xpath:
$xml_str = file_get_contents('salespage.xml');
$xml = new SimpleXMLElement($xml_str);
$items = $xml->xpath('/*/transfer');
print_r($items);
foreach($items as $item) {
echo $item->buyerTeamname, ': ', $item->sellerTeamname, "\n";
}
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
Is this what you want?

Categories