Reputation: 1017
<html>
<head>
<?PHP
include('simple_html_dom.php');
?>
<title>
</title>
</head>
<body>
<form name ="form1" method ="POST" ACTION="parser.php">
<input type="text" name="parser1" style="height:200px; width:200pt;"></br></br>
<input type="submit" value="Submit"></br></br>
</form>
<?php
$html_str = $_POST['parser1'];
// Create DOM from URL or file
$html = file_get_html($html_str);
$html->load('
<form name="form1" action="parser.php" method="post">
<input type="text" name="parser1">
</form>');
// Get the form action
foreach($html->find('form') as $element)
echo $element->action . '<br>';
// Get the input name
foreach($html->find('input') as $element)
echo $element->name . '<br>';
?>
</body>
</html>
Here i am trying to enter the html source into the text box parser1
I am then catching the data from the textbox using post into a string html_str
when i try to parse that string, i start getting errors.
Fatal error: Call to a member function load() on a non-object in /home/public_html/parser.php on line 24
please help
Upvotes: 0
Views: 903
Reputation: 146660
You have this:
$html = file_get_html($html_str);
$html->load('
<form name="form1" action="parser.php" method="post">
<input type="text" name="parser1">
</form>');
The error message says that $html
is not an object. file_get_html()
isn't a builtin function but you appear to be using PHP Simple HTML DOM Parser. Its API documentation says that it returns an object but fails to provide additional info. If we look at the source code:
function file_get_html($url, $use_include_path = false, $context=null, $offset = -1, $maxLen=-1, $lowercase = true, $forceTagsClosed=true, $target_charset = DEFAULT_TARGET_CHARSET, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT)
{
// We DO force the tags to be terminated.
$dom = new simple_html_dom(null, $lowercase, $forceTagsClosed, $target_charset, $stripRN, $defaultBRText, $defaultSpanText);
// For sourceforge users: uncomment the next line and comment the retreive_url_contents line 2 lines down if it is not already done.
$contents = file_get_contents($url, $use_include_path, $context, $offset);
// Paperg - use our own mechanism for getting the contents as we want to control the timeout.
//$contents = retrieve_url_contents($url);
if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
{
return false;
}
// The second parameter can force the selectors to all be lowercase.
$dom->load($contents, $lowercase, $stripRN);
return $dom;
}
... we can see it returns FALSE
some times:
if (empty($contents) || strlen($contents) > MAX_FILE_SIZE)
{
return false;
}
So I'd dare say that your POST field is either empty or too large. You should really check that before calling ->load()
.
Update:
The file_get_html()
function:
Creates a DOM object from a file or a URL.
I guess what you really want is str_get_html()
:
Creates a DOM object from a string.
Upvotes: 1
Reputation: 13853
It might help if you actually check if the form is submitted or not. You have to check and re-check if the input is valid or not.
// check if it's a POST request
if($_SERVER['REQUEST_METHOD'] === 'POST') {
// check if parser1 is not empty
if(!empty($_POST['parser1'])) {
$input = $_POST['parser1'];
if(filter_var($input, FILTER_VALIDATE_URL)) { // looks like an URL
$html = file_get_html($input); // download URL
} else { // lets assume it's HTML, because it's not an URL
$html = str_get_html($input);
}
// If something goes wrong here, the input is invalid
if(!empty($html)) {
// parse DOM document here
} else {
// There is something wrong with the input
}
}
}
Upvotes: 0