can't load simple-html-dom class two times in one class - php

When I try to load the file in a dom object for the secont time(i clear it befor), the dom variable is empty :(
How can I use simple-html-dom class for two times in my class?
<?php
require_once 'simple_html_dom.php';
class RO{
public $dom;
public $dom2;
public function __construct(){
$this->dom = new simple_html_dom();
$this->dom2 = new simple_html_dom();
}
function GetDatumInUroPodatkovInTabKrajev($lang){
$this->dom->load_file("http://www...diferentadresthenbelow...html");
$tabelaTempInKrajev = $this->dom->find('table[#class="online"]');
//some code...
//some code...
return $this->functon2("minkjaaa");
}
function functon2($ik){
$this->dom2->load_file("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html");
$tabelaVremRazm = $this->dom2->find('table[#class="online"]'); // this line trows an error, becaus dom2 is empty
return"";
}
}
?>
and the trowen error:
[Fatal error: Call to a member function find() on a non-object in C:\..\simple_html_dom.php on line ..]

Find("SELECTORS", index) Find returns an array of objects unless an optional index is specified, then it will return the Nth object... This is probably what you're missing... Here's a working example:
// includes Simple HTML DOM Parser
include "simple_html_dom.php";
$url = "http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_si.html";
//Create a DOM object
$dom2 = new simple_html_dom();
// Load HTML from a string
$dom2->load_file($url);
// Find the link with the appropriate selectors
$table = $dom2->find('table.online', 0);
// Find succeeded
if ($table)
echo $table->outertext;
else
echo "Find function failed !";
// Clear DOM object (needed essentially when using many)
$dom2->clear();
unset($dom2);
Online DEMO

Related

PHP: Load a class in a class

I have a class which is meant to "load" an another class, however I haven't been able to get it to work.
Error Message
Fatal error: Call to undefined method stdClass::echoString() in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\classes\example.php on line 5
Code
My code is broken up into three main sections:
api.php - the class to load the other classes.
API/exampleExternalAPI.php - (multiple files) the classes that api.php loads
example.php - the file that uses the main class (api.php)
If it helps these files can be downloaded from my dropbox
api.php
<?php
/* Config */
define('pathToAPIs','API/');
/* Autoload Function */
spl_autoload_register(function($className){
$namespace=str_replace("\\","/",__NAMESPACE__);
$className=str_replace("\\","/",$className);
$class=pathToAPIs.(empty($namespace)?"":$namespace."/")."{$className}.php";
include_once($class);
});
class api {
private $listOfAPIs;
public $APIs;
public function __construct($setAPI = null){
$this->updateListOfAPIs();
if (isset($setAPI)){
return $this->setAPI($setAPI);
}
}
public function setAPIs($setAPIs){
$this->APIs = null; // clears a previous call to this method
if (!is_array($setAPIs)){ // if not an array
$setAPIs = array($setAPIs); // make array
}
foreach ($setAPIs as $setAPIType){
if(in_array($setAPIType,$this->listOfAPIs)){
$array[$setAPIType] = new $setAPIType;
}
}
$this->APIs = json_decode(json_encode($array), FALSE); // convert array of required api objects to an object
return $this->APIs;
}
public function getListOfAPIs($update = false){
if ($update){
$this->updateListOfAPIs();
}
return $this->listOfAPIs;
}
private function updateListOfAPIs(){
$this->listOfAPIs = null; // clears a previous call to this method
$it = new FilesystemIterator(pathToAPIs);
foreach ($it as $fileinfo){
$filename = pathinfo($fileinfo->getFilename(), PATHINFO_FILENAME); // removes extension
$this->listOfAPIs[]= $filename;
}
}
public function __call($method,$args){
}
}
API/exampleExternalAPI.php
<?php
class exampleExternalAPI {
public function echoString($string){
echo $string;
}
}
example.php
<?php
require_once 'api.php';
$api = new api();
$api->setAPIs('exampleExternalAPI');
$api->APIs->exampleExternalAPI->echoString('string');
Background Info
(may give some insight to my madness)
I'm working on a project where I need to connect to lots of external APIs.
So I decided to creating a class to look after all my communications with external APIs ( not sure if best way - new to Object Oriented Programming).
I'm not entirely sure what problem you're trying to solve, but if your APIs is a simple stdClass instance it should work as expected:
public function setAPIs($setAPIs)
{
$this->APIs = new stdClass; // clears a previous call to this method
if (!is_array($setAPIs)) { // if not an array
$setAPIs = array($setAPIs); // make array
}
foreach ($setAPIs as $setAPIType) {
if (in_array($setAPIType, $this->listOfAPIs)) {
$this->APIs->{$setAPIType} = new $setAPIType;
}
}
return $this->APIs;
}

PHP DomDocument appendChild() on a non object

I have a PHP class that is used to generate some HTML in the following way:
public $rName;
public $cName;
public $rMonth;
function __construct(){
$this->report = new DOMDocument;
$this->report->loadHTMLFile('template.php');
}
private function addComponent($tag, $content){
$parent = $this->report->getElementById('content');
$child = $this->report->createElement($tag, $content);
$parent->appendChild($child);
}
function addSection($header){
$this->addComponent('h2', $header);
}
function addSubHeader($subHeader){
$this->addComponent('h3', $subHeader);
}
function addContent($content){
$this->addComponent('p', $content);
}
which is being called like this:
$report = new Report;
$outputType = $_GET['outputType'];
$report->rName = 'rName';
$report->cName = $_GET['cName'];
$report->rMonth = $_GET['rMonth'];
$report->addSection('Section');
$report->addSubHeader('SubHeader');
$report->addContent('Content');
Using XAMPP on Windows, this code works absolutely fine. However on a centos environment I get the error:
Call to a member function appendChild() on a non-object on line 16
Line 16 is:
$parent->appendChild($child);
The template.php file appears to be loading, and there is a div with the id of "content", however a gettype() on $parent shows it as NULL.
Pretty stumped at the moment. Any ideas?
In older versions of PHP, getElementById requires that an id attribute has been specified in a DTD.
Inserting <!DOCTYPE html> at the start of the HTML may be enough, in this case.

Way to redeclare file_get_html()?

I'm using an html parser to scrape html and then format it so that it can be inserted to the DB.
require_once('simple_html_dom.php');
$url = "http://www.site.com/url/params/"
$html = file_get_html($url);
// The team links are stored in the div.thumbHolder
foreach($html->find('.logoWall li') as $e)
{
ob_start();
sportPics($e, $league);
}
The sportsPics() function is this:
function sportsPic()
{
require('simple_html_dom.php');
foreach($e->find('a') as $a)
{
// Execute code
}
}
I get an error reading:
Fatal error: Cannot redeclare file_get_html()
I thought changing require() to require_once() and vice versa would work. But, it didn't. I also thought a buffer might work but I don't know too much about how they work.
Don't do this again -
require('simple_html_dom.php');
in sportsPic() function.
Update - Your function definition function sportsPic() takes no argument. But look at this line -
sportPics($e, $league);
Redefine your function to take arguments.
You are passing arguments but the function has no way to access them as it takes no arguments. And hence, your $e is a non-object.
Put this out sided sportsPic() function
require('simple_html_dom.php');
you are calling this again & again in a loop.
foreach($html->find('.logoWall li') as $e){
foreach($e->find('a') as $a){
// Execute code
}
}

How to serialize/save a DOMElement in $_SESSION?

I'm pretty new to PHP, DOM, and the PHP DOM implementation. What I'm trying to do is save the root element of the DOMDocument in a $_SESSION variable so I can access it and modify it on subsequent page loads.
But I get an error in PHP when using $_SESSION to save state of DOMElement:
Warning: DOMNode::appendChild() [domnode.appendchild]: Couldn't fetch DOMElement
I have read that a PHP DOMDocument object cannot be saved to $_SESSION natively. However it can be saved by saving the serialization of the DOMDocument (e.g. $_SESSION['dom'] = $dom->saveXML()).
I don't know if the same holds true for saving a DOMElement to a $_SESSION variable as well, but that's what I was trying. My reason for wanting to do this is to use an extended class of DOMElement with one additional property. I was hoping that by saving the root DOMElement in $_SESSION that I could later retrieve the element and modify this additional property and perform a test like, if (additionalProperty === false) { do something; }. I've also read that by saving a DOMDocument, and later retrieving it, all elements are returned as objects from native DOM classes. That is to say, even if I used an extended class to create elements, the property that I subsequently need will not be accessible, because the variable holding reference to the extended-class object has gone out of scope--which is why I'm trying this other thing. I tried using the extended class (not included below) first, but got errors...so I reverted to using a DOMElement object to see if that was the problem, but I'm still getting the same errors. Here's the code:
<?php
session_start();
$rootTag = 'root';
$doc = new DOMDocument;
if (!isset($_SESSION[$rootTag])) {
$_SESSION[$rootTag] = new DOMElement($rootTag);
}
$root = $doc->appendChild($_SESSION[$rootTag]);
//$root = $doc->appendChild($doc->importNode($_SESSION[$rootTag], true));
$child = new DOMElement('child_element');
$n = $root->appendChild($child);
$ct = 0;
foreach ($root->childNodes as $ch) echo '<br/>'.$ch->tagName.' '.++$ct;
$_SESSION[$rootTag] = $doc->documentElement;
?>
This code gives the following errors (depending on whether I use appendChild directly or the commented line of code using importNode):
Warning: DOMNode::appendChild() [domnode.appendchild]: Couldn't fetch DOMElement in C:\Program Files\wamp_server_2.2\www\test2.php on line 11
Warning: DOMDocument::importNode() [domdocument.importnode]: Couldn't fetch DOMElement in C:\Program Files\wamp_server_2.2\www\test2.php on line 12
I have several questions. First, what is causing this error and how do I fix it? Also, if what I'm trying to do isn't possible, then how can I accomplish my general objective of saving the 'state' of a DOM tree while using a custom property for each element? Note that the additional property is only used in the program and is not an attribute to be saved in the XML file. Also, I can't just save the DOM back to file each time, because the DOMDocument, after a modification, may not be valid according to a schema I'm using until later when additional modificaitons/additions have been performed to the DOMDocument. That's why I need to save a temporarily invalid DOMDocument. Thanks for any advice!
EDITED:
After trying hakre's solution, the code worked. Then I moved on to trying to use an extended class of DOMElement, and, as I suspected, it did not work. Here's the new code:
<?php
session_start();
//$_SESSION = array();
$rootTag = 'root';
$doc = new DOMDocument;
if (!isset($_SESSION[$rootTag])) {
$root = new FreezableDOMElement($rootTag);
$doc->appendChild($root);
} else {
$doc->loadXML($_SESSION[$rootTag]);
$root = $doc->documentElement;
}
$child = new FreezableDOMElement('child_element');
$n = $root->appendChild($child);
$ct = 0;
foreach ($root->childNodes as $ch) {
$frozen = $ch->frozen ? 'is frozen' : 'is not frozen';
echo '<br/>'.$ch->tagName.' '.++$ct.': '.$frozen;
//echo '<br/>'.$ch->tagName.' '.++$ct;
}
$_SESSION[$rootTag] = $doc->saveXML();
/**********************************************************************************
* FreezableDOMElement class
*********************************************************************************/
class FreezableDOMElement extends DOMElement {
public $frozen; // boolean value
public function __construct($name) {
parent::__construct($name);
$this->frozen = false;
}
}
?>
It gives me the error Undefined property: DOMElement::$frozen. Like I mentioned in my original post, after saveXML and loadXML, an element originally instantiated with FreezableDOMElement is returning type DOMElement which is why the frozen property is not recognized. Is there any way around this?
You can not store a DOMElement object inside $_SESSION. It will work at first, but with the next request, it will be unset because it can not be serialized.
That's the same like for DOMDocument as you write about in your question.
Store it as XML instead or encapsulate the serialization mechanism.
You are basically facing three problems here:
Serialize the DOMDocument (you do this to)
Serialize the FreezableDOMElement (you do this to)
Keep the private member FreezableDOMElement::$frozen with the document.
As written, serialization is not available out of the box. Additionally, DOMDocument does not persist your FreezableDOMElement even w/o serialization. The following example demonstrates that the instance is not automatically kept, the default value FALSE is returned (Demo):
class FreezableDOMElement extends DOMElement
{
private $frozen = FALSE;
public function getFrozen()
{
return $this->frozen;
}
public function setFrozen($frozen)
{
$this->frozen = (bool)$frozen;
}
}
class FreezableDOMDocument extends DOMDocument
{
public function __construct()
{
parent::__construct();
$this->registerNodeClass('DOMElement', 'FreezableDOMElement');
}
}
$doc = new FreezableDOMDocument();
$doc->loadXML('<root><child></child></root>');
# own objects do not persist
$doc->documentElement->setFrozen(TRUE);
printf("Element is frozen (should): %d\n", $doc->documentElement->getFrozen()); # it is not (0)
As PHP does not so far support setUserData (DOM Level 3), one way could be to store the additional information inside a namespaced attribute with the element. This can also be serialized by creating the XML string when serializing the object and loading it when unserializing (see Serializable). This then solves all three problems (Demo):
class FreezableDOMElement extends DOMElement
{
public function getFrozen()
{
return $this->getFrozenAttribute()->nodeValue === 'YES';
}
public function setFrozen($frozen)
{
$this->getFrozenAttribute()->nodeValue = $frozen ? 'YES' : 'NO';
}
private function getFrozenAttribute()
{
return $this->getSerializedAttribute('frozen');
}
protected function getSerializedAttribute($localName)
{
$namespaceURI = FreezableDOMDocument::NS_URI;
$prefix = FreezableDOMDocument::NS_PREFIX;
if ($this->hasAttributeNS($namespaceURI, $localName)) {
$attrib = $this->getAttributeNodeNS($namespaceURI, $localName);
} else {
$this->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:' . $prefix, $namespaceURI);
$attrib = $this->ownerDocument->createAttributeNS($namespaceURI, $prefix . ':' . $localName);
$attrib = $this->appendChild($attrib);
}
return $attrib;
}
}
class FreezableDOMDocument extends DOMDocument implements Serializable
{
const NS_URI = '/frozen.org/freeze/2';
const NS_PREFIX = 'freeze';
public function __construct()
{
parent::__construct();
$this->registerNodeClasses();
}
private function registerNodeClasses()
{
$this->registerNodeClass('DOMElement', 'FreezableDOMElement');
}
/**
* #return DOMNodeList
*/
private function getNodes()
{
$xp = new DOMXPath($this);
return $xp->query('//*');
}
public function serialize()
{
return parent::saveXML();
}
public function unserialize($serialized)
{
parent::__construct();
$this->registerNodeClasses();
$this->loadXML($serialized);
}
public function saveBareXML()
{
$doc = new DOMDocument();
$doc->loadXML(parent::saveXML());
$xp = new DOMXPath($doc);
foreach ($xp->query('//#*[namespace-uri()=\'' . self::NS_URI . '\']') as $attr) {
/* #var $attr DOMAttr */
$attr->parentNode->removeAttributeNode($attr);
}
$doc->documentElement->removeAttributeNS(self::NS_URI, self::NS_PREFIX);
return $doc->saveXML();
}
public function saveXMLDirect()
{
return parent::saveXML();
}
}
$doc = new FreezableDOMDocument();
$doc->loadXML('<root><child></child></root>');
$doc->documentElement->setFrozen(TRUE);
$child = $doc->getElementsByTagName('child')->item(0);
$child->setFrozen(TRUE);
echo "Plain XML:\n", $doc->saveXML(), "\n";
echo "Bare XML:\n", $doc->saveBareXML(), "\n";
$serialized = serialize($doc);
echo "Serialized:\n", $serialized, "\n";
$newDoc = unserialize($serialized);
printf("Document Element is frozen (should be): %s\n", $newDoc->documentElement->getFrozen() ? 'YES' : 'NO');
printf("Child Element is frozen (should be): %s\n", $newDoc->getElementsByTagName('child')->item(0)->getFrozen() ? 'YES' : 'NO');
It's not really feature complete but a working demo. It's possible to obtain the full XML without the additional "freeze" data.

PHP: Create new node inside parent

I have a root XML node called and I am trying to add a new child called to this but I am getting errors. Inside there is also children. Here is my code:
$xml = new DomDocument();
$xml->load(X_ASSETS);
$xml->formatOutput = true;
$new_id = $this->getNewAssetId();
// Root
$xpath = new DOMXPath($xml);
$assets = $xpath->query('assets');
$xml_assets = $assets->item(0);
$xml_root = $xml->createElement('asset');
// Asset Name
$xml_name = $xml->createElement('name');
$xml_name->nodeValue = $clean_name;
$xml_root->appendChild($xml_name);
// Asset URL
$xml_url = $xml->createElement('url');
$xml_url->nodeValue = '/'.$name;
$xml_root->appendChild($xml_url);
// Asset ID
$xml_id = $xml->createElement('id');
$xml_id->nodeValue = $new_id;
$xml_root->appendChild($xml_id);
// Create our document and save
$xml_assets->appendChild($xml_root);
$xml->save(X_ASSETS);
I get the following error when running this:
Fatal error: Call to a member function appendChild() on a non-object in /home/websites/zed_x/core/includes/x.inc on line 88
Does anyone know what I am doing wrong here?
Somehow your $xml_assets is not an object, and therefore you cannot call the function:
$xml_assets->appendChild($xml_root);
Are you certain positive that the following command returns an object?
$xml_assets = $assets->item(0);
Test it:
if(is_object($xml_assets))
{
echo "Object Here!";
}
This might be a good way to structure your code so you can catch errors
// .... stuff .....
$xml_assets = $assets->item(0);
// ... more stuff ....
// Check for Object
if(!is_object($xml_assests))
{
die("No Object Created!");
}
$xml_assets->appendChild($xml_root);
$xml->save(X_ASSETS);
// .... more stuff .....

Categories