PHP error "Call to undefined function" using simple html dom - php

I'm fairly new to PHP, and i have a problem in defining a function that returns an array containing a price and description strings.
I am using the "simple html dom" php files that facilitates parsing.
The function i create requires 2 arguments : the link (from which it will grab data) and the id (used to get the proper css syntax).
This is the get_product_details.php
<?
require_once 'simple_html_dom.php';
$priceMatchTable=('span[id=our_price_display]');
$descMatchTable=('div[id=short_description_content]');
function get_prod_details( $link , $id ) {
global $priceMatchTable, $descMatchTable;
$html = file_get_html($link);
$result['price'] = $html->find($priceMatchTable[$id],0);
$result['desc'] = $html->find($descMatchTable[$id],0);
return $result;
}
And this is the main php:
<?php
include 'get_product_details.php';
$link = 'http://micromedia.tn/barette-memoire/1170-barette-m%C3%A9moire-1go-ddr-ii.html';
$id = 0;
$result = get_prod_details($link, $id);
echo $result['price'];
?>
Finally i get an error which tell:
find($priceMatchTable[$id],0); $result['desc'] = $html->find($descMatchTable[$id],0); return $result; }
Fatal error: Call to undefined function get_prod_details() in C:\xampp\htdocs\dom\index.php on line 8
Best regards!

This may sound silliy, but is
include 'get_product_details.php';
really pointing towards "get_product_details.php"?
Disable (//) the function call in you index.php and add a simple echo to your "get_product_details.php" to see if the file gets included.

I think you need something like:
include '/path/from/root_to_your/directory/get_product_details.php';
If your trying this in Windows land, it will look something like:
include 'C:\Documents\something\get_product_details.php';

Related

Alternative for eval() -PHP

i am trying to implement URL mapping in PHP. I have a json file which stores the url and functions which is to execute when that link is requested. I was using eval() but then i came across this
Kepp the following Quote in mind:
If eval() is the answer, you're almost certainly asking the wrong
question. -- Rasmus Lerdorf, BDFL of PHP
now i am thinking is their any other(better) way to do it.
My json file looks like this.
{
"bw/":"main()",
"bw/login":"login()"
}
and my loadPage function look like this.
function loadPage($url){ //$url = 'bw/'
$str = file_get_contents('urls.json');
$this->link = json_decode($str, true);
$url = ltrim($url,"/");
$key = $this->link[$url];
eval("$key;");
}
EDIT:
i defined $this->link in my code
A slight tweak to your JSON to allow you to call the function dynamically would make it easier, just remove the brackets so it would look like...
{
"bw/":"main",
"bw/login":"login"
}
and then call it using...
function loadPage($url){ //$url = 'bw/'
$url = ltrim($url,"/");
$key = $this->link[$url];
$key();
}
A little better way is changing eval() to:
if (function_exists($key)) {
return $key();
}
return default();
and you might create a function "default" to show an error 404 or default page when function doesn't exists.

Error in parsing html content using DOMDocument of PHP

Can anyone please help me in finding out what have I done wrong in the code given below.
I have a PHP variable named news_content whose value is the following html...
<p><img src="./images/image1.jpeg" alt=""></p>
This value for variable news_content is obtained from a database query.
The function below creates a DOMDocument object using the variable news_content :
public function convert_to_tinymce_data($news_content)
{
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link)
{
$img_link=$link->getAttribute('src');
echo $link->getAttribute('src');
}
}
But nothing is being echoed (receiving a blank page).
I've tryed this code and it works:
$n = '<p><img src="./images/image1.jpeg" alt=""></p>';
function convert_to_tinymce_data($news_content){
$dom=new DOMDocument();
$dom->loadHTML($news_content);
$img_nodes=$dom->getElementsByTagName('img');
foreach($img_nodes as $link) {
$img_link=$link->getAttribute('src');
echo $img_link;
}
}
convert_to_tinymce_data($n);
I see you have public function, but I don't see class. If it's global function, not a method of a class, that's the reason why it doesn't work. Or you don't call function properly.
(What you have there is function definition... it executes after you call it... Having same variable names in your function's definition and outside is bad practice and can be easily turn into problem)
Just to add: Check you're error_log file and let us know is there something interesting in it...

a function for creating shortname style variables in PHP

I'm just learning PHP, and I'm sure I'm not the first person to try something like this, but maybe because I didn't know what to call it, I couldn't find any other examples or questions on here or from a search on google.
I wanted to create a function which takes two parameters and uses them to create all the short style variables I'll be using later in my script.
My problem is where normally the syntax to create a short variable is something like:
$var = $_METHOD['var'];
but I'm trying to do this using variables and don't know how to get the syntax right. Here's my code:
<?php
session_start();
function shortnames($some_vars, $type) {
for ($i = 0; $i<(count($some_vars)); $i++) {
$$some_vars[$i] = $_$type['$some_vars[$i]'];
echo $$some_vars[$i].' = '.$some_vars[$i].'<br />';
}
}
$post_vars = array(lastname,firstname,payment);
$session_vars = array(sessiontxt,province,city,venue,regfee);
shortnames($session_vars,session);
shortnames($post_vars,post);
?>
Thanks for helping a beginner!!

getting an error reading simpleDomObject

I have the following template file, named 'test.html'
<div class='title'>TEST</div>
And I have the following PHP code:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
echo $dom->outertext;
?>
So far so good, this displays the file test.html. But when I try to change something I get an error:
<?
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "test.html" );
$dom->find('.title')->innertext = "changed";
echo $dom->outertext;
?>
Warning: Attempt to assign property of non-object in E:\internet\test.php on line 4. Though I do believe I'm exactly following the manual. What is going wrong here? Obviously $dom->find('.title') didn't return a valid element, but the question is: why? It should find the DIV?
First:
You obviously missed index for found elements, so there isn't property find()->innertext
repaired code here:
<?php
error_reporting(E_ALL);
include "simplehtmldom/simple_html_dom.php";
$dom = file_get_html( "index.html" );
$dom->find('.title',0)->innertext = "changed";
echo $dom->outertext;
Second:
I wouldn't recommend you to use Simple Html DOM library, beacuse it's old and not actual
Take a look at QueryPath library, which is doing the same and is in better condition.
http://querypath.org/

php Object to String

I'm trying to teach myself php... so please be kind and bear with me.
I'm trying to follow this tutorial on how to cache files... the page I want to cache is HTML only, so I've modified the php to just deal with data. I know the caching part is working, it's when I try to modify the results that I get a "Catchable fatal error: Object of class Caching could not be converted to string" in the str_replace line below.
I've tried using the __toString method here, and I've tried using serialize. Is there something I'm missing?
Edit: Oh and I've even tried casting operators.
$caching = new Caching( "my.htm", "http://www.page-I-want.com/" );
$info = new TestClass($caching);
$info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );
My var_dump($caching); is as follows:
object(Caching)#1 (2) { ["filePath"]=> string(9) "cache.htm" ["apiURI"]=> string(27) "http://www.page-I-want.com/" }
Ok, I see now that the problem is with the caching.php not returning the value to the $caching string. Can anyone check out the link below and help me figure out why it's not working? Thanks!
I just posted my entire caching.php file here.
The code in on the site you link works by downloading the page from URL you give and parse it for artists and then save them to the cache file. The cache-object only contains two variables; filePath and apiURI. If you want to modify how the page is parse and converted to the cached XML-file, you should change the stripAndSaveFile function.
Here is an example of how to modify the Caching.php to do what you wanted:
function stripAndSaveFile($html) {
//mange the html code in any way you want
$modified_html = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $html );
//save the xml in the cache
file_put_contents($this->filePath, $modified_html);
}
Edit:
Other option is to extend the Caching class, in your php-code using the class you could do:
class SpecialCaching extends Caching {
var $html = "";
function stripAndSaveFile($html) {
//mange the html code in any way you want
$this->html = $html;
}
}
$caching = new SpecialCaching( "my.htm", "http://www.page-I-want.com/" );
$info = $caching->html;
$info = str_replace( "<img src='/images/up.jpg'>","<div class='up'></div>", $info );

Categories