file_get_html displays Fatal Error Call to undefined function
are you sure you have downloaded and included php simple html dom parser ?
Uncaught Error: Call to undefined function get_file_html()
You can use str_get_html()
with the response from the curl request you have, so the code would look something like...
include "simple_html_dom.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://in.tradingview.com/symbols/NSE-TCS/");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$html = str_get_html($response);
foreach($html->find('a') as $element) {
echo $element->href . " -> " . $element->plaintext .'<br>';
}
file_get_html - Internal Server Error
PHP Warning: file_get_contents(google.com): failed to open stream:
php_network_getaddresses: getaddrinfo failed: Temporary failure in
name resolution
This tells that your PHP cannot, fails or is not allowed to connect to internet (dns server) to resolve site google.com. You should contact your service provider to have them clarify why this is so. It's not something you can do anything about in your code (except to try using direct IP, but you should really contact them to have this resolved, as it probably affects elsewhere as well).
Simple HTML DOM parsing not working
You mix Simple HTML Dom third part class commands (as per your question title) with DOMDocument built-in class commands, so your code can't work.
file_get_html()
is a Simple HTML Dom function, replace it with file_get_contents()
:
$html = file_get_contents( '/Users/sam/Downloads/trash.html' );
$dom = new DOMDocument();
libxml_use_internal_errors( 1 ); // <-- add this line to avoid DOM errors
$dom->loadHTML( $html );
$elements = $dom->getElementsByTagName('tr');
Now, init an array ($rows
) to fill with cells values and a integer string ($cols
) for column numbers; your HTML is malformed and this variable will help you to produce a well-formed table:
$rows = array();
$cols = 0;
In your code there is another error: you put <tr>
in $elements
, then you refer it in foreach()
using $rows
. Then, you call ->children()
method to iterate through all children, but DOMElement don't have this method, use ->childNodes
property instead. But, first to all, check the row column number and update previously declared variable $cols
. Inside nested foreach()
, you add cells values to $rows
. You will display later. To retrieve values of DOMNode use ->nodeValue
instead of ->plaintext
. I have wrapped $cell->nodeValue
by trim()
to remove extra spaces at begin/end of string:
foreach ($elements as $key => $row)
{
if( $row->childNodes->length > $cols ) $cols = $row->childNodes->length;
foreach( $row->childNodes as $cell )
{
$rows[$key][] = trim( $cell->nodeValue );
}
}
Now, you have the cells values in multidimensional array $rows
.
Table display
Your code for displaying table is not your code, it is a copy-and-paste from the net: it has nothing to do with your question and you can ignore it.
Use a simply code like this instead:
echo "<table>\n";
echo " <tr>\n";
for( $j = 0; $j < $cols; $j++ ) echo " <th>{$rows[0][$j]}</th>\n";
echo " </tr>\n";
for( $i = 1; $i < count($rows); $i++ )
{
echo " <tr>\n";
for( $j = 0; $j < $cols; $j++ )
{
if( isset( $rows[$i][$j] ) ) echo " <td>{$rows[$i][$j]}</td>\n";
else echo " <td></td>\n";
}
echo " </tr>\n";
}
echo "</table>\n";
This is only a working example, modify HTML code as you prefer. You can also change the order of cells. Note the different code between printing table header and printing table rows (for()
loop start from 1). Also note the use of $cols
: if a cell is empty, we output an empty <td>
.
Caught fatal error: Call to undefined function geoip_db_avail()?
Manual is your friend. This function is declared in PECL geoip
extension which is installed on one of your servers but not on another.
You can read here about installation and requirements.
Declaring function in foreach loop
please change code few line
foreach ($links as $link) {
require('simple_html_dom.php'); **// here** comment line like **//require('simple_html_dom.php');**
$html = file_get_html('http://www.forgolf.cz'. $link);
foreach ($html-find('.item-page p') as $p) {
if (is_string($p))
$paragraph[] = $p->innertext;
}
}
?>
Finally Code
<?php
require ('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach($html->find('h2 a') as $title)
{
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach($html->find('.item-inner p img') as $img)
{
$img_src[] = $img->getAttribute('src');
}
foreach($links as $link)
{
// require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz' . $link);
foreach($html->find('.item-page p') as $p)
{
if (is_string($p)) $paragraph[] = $p->innertext;
}
}
?>
Related Topics
Invoke External Shell Script from PHP and Get Its Process Id
PHP - Returning the Last Line in a File
How to Compare Two Time in PHP
How to Get Page Number on Dompdf PDF When Using "View"
Capture Newline from a Textarea Input
Getting Multiple Checkboxes Names/Id's with PHP
Validating Us Phone Number with PHP/Regex
How to Override Built-In PHP Function(S)
Php: iPad Does Not Play Mp4 Videos Delivered by PHP, But If Accessed Directly It Does
Remove Trailing Slash from String PHP
How to Serve Documents from Outside the Web Root Using PHP
How to Perform an Action Every 5 Results
Error 1148 MySQL the Used Command Is Not Allowed with This MySQL Version
Why PHP Variables Start with a $ Sign Symbol
Stored Procedures, MySQL and PHP