How can I fix this error?

I'm using file_get_contents and file_put_contents in the context of an app that I'm building. I've been able to successfully get the content and write to a new html file, but when I go and try to retrieve the content from the page using loadHTML(), I'm getting an error and I'm not sure where it's coming from.

Here's my code:

<?php
libxml_use_internal_errors(true);
if (file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html')) {
$datafile14 = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');
echo "Uploading Filename: https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html\n";
file_put_contents('raw_html/crystal.html',$datafile14);
//chmod("raw_html/crystal.html",0777);
echo "data has been uploaded!";
}
$html = file_get_contents('raw_html/crystal.html');


    /*** a new dom object ***/ 
    $dom = new domDocument; 

    /*** load the html into the object ***/ 
    $dom->loadHTML($html); 
	if(!$dom) 
	{
	echo "you've got a problem...!";
	}

    /*** discard white space ***/ 
    $dom->preserveWhiteSpace = false; 

    /*** the table by its tag name ***/ 
    $tables = $dom->getElementsByTagName('table'); 

    /*** get all rows from the table ***/ 
    $rows = $tables->item(0)->getElementsByTagName('tr'); 

    /*** loop over the table rows ***/ 
    foreach ($rows as $row) { 
        /*** get each column by tag name ***/ 
$cols = $row->getElementsByTagName('td'); 

$enddate = $cols->item[0]->nodeValue;
$num_days = $cols->item[1]->nodeValue;
$area = $cols->item[2]->nodeValue;
$mmepool = $cols->item[3]->nodeValue;
$region = $cols->item[4]->nodeValue;
$vendor = $cols->item[5]->nodeValue;
$volte_ia_pcnt = $cols->item[6]->nodeValue;
$volte_lc_pcnt = $cols->item[7]->nodeValue;
$sip_mou = $cols->item[8]->nodeValue;
$volte_qci1_setup_failure_pcnt = $cols->item[9]->nodeValue;
$volte_qci1_setup_failures = $cols->item[10]->nodeValue;
$volte_qci1_setup_attempts = $cols->item[11]->nodeValue;
$qci5_setup_failure_pcnt = $cols->item[12]->nodeValue;
$qci5_setup_failures = $cols->item[13]->nodeValue;
$qci5_setup_attempts = $cols->item[14]->nodeValue;
$seer_pcnt = $cols->item[15]->nodeValue;
$seer_sip_ntwk_eff_calls = $cols->item[16]->nodeValue;
$seer_sip_call_attempts = $cols->item[17]->nodeValue;
$rrc_setup_failure_pcnt = $cols->item[18]->nodeValue;
$rrc_setup_attempts = $cols->item[19]->nodeValue;
$rrc_setup_failures = $cols->item[20]->nodeValue;
$volte_qci1_drop_pcnt = $cols->item[21]->nodeValue;
$volte_qci1_drops = $cols->item[22]->nodeValue;
$volte_qci1_established_calls = $cols->item[23]->nodeValue;
$qci5_bearer_drop_pcnt = $cols->item[24]->nodeValue;
$qci5_bearer_drops = $cols->item[25]->nodeValue;
$qci5_bearer_estab_calls = $cols->item[26]->nodeValue;
$volte_avg_mos = $cols->item[27]->nodeValue;
$volte_mos_numerator = $cols->item[28]->nodeValue;
$volte_mos_denominator = $cols->item[19]->nodeValue;
$volte_pdcp_dl_volume_mb = $cols->item[30]->nodeValue;
$volte_dl_volume_pcnt = $cols->item[31]->nodeValue;
$lte_pdcp_dl_volume_mb = $cols->item[32]->nodeValue;
$x7x24_volte_pdcp_dl_volume_mb = $cols->item[33]->nodeValue;
$x7x24_volte_dl_volume_pcnt = $cols->item[34]->nodeValue;
$x7x24_lte_pdcp_dl_volume_mb = $cols->item[35]->nodeValue;
$volte_pdcp_ul_volume_mb = $cols->item[36]->nodeValue;
$volte_ul_volume_pcnt = $cols->item[37]->nodeValue;
$lte_pdcp_ul_volume_mb = $cols->item[38]->nodeValue;
$x7x24_days = $cols->item[39]->nodeValue;
}
echo $x7x24_days;
?>

Open in new window


The error I'm getting is: "Notice: Undefined property: DOMNodeList::$item in /opt/lampp/htdocs/crystal/test.php on line 37."

After that I get the expected parade of errors as my code tries to retrieve data from what is perceived as a "non-object."

What am I missing?
brucegustPHP DeveloperAsked:
Who is Participating?
 
GaryConnect With a Mentor Commented:
Just a small change to skip the first row which are TH's as Hain guessed.

foreach($tables->getElementsByTagName('tr') as $index=>$row)
{
    if($index>0){
        ...
    }
}

Open in new window

0
 
GaryCommented:
Are you sure you are getting a webpage as that link errors out.
0
 
brucegustPHP DeveloperAuthor Commented:
Hey, Gary!

I'm not sure if you would be able to see the page just because it's an intranet thing, but the attached screenshot is a look at "crystal.html," so I am getting data.

screenshot
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
GaryCommented:
Ok, if you load the file directly (don't bother saving it)

$html = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');

Open in new window


Is it still the same?
0
 
HainKurtSr. System AnalystCommented:
are you getting error on this line?

$enddate = $cols->item[0]->nodeValue;

does it make any difference if you use textContent instead?
what html do you get? can you please post the table and a few tr...
0
 
brucegustPHP DeveloperAuthor Commented:
Yep! Exact same errors.
0
 
brucegustPHP DeveloperAuthor Commented:
Hain...

I am getting an error on that line.

Here's some sample data from the HTML page that's being accessed...

<table border="1">
<tr><th>EndDate</th><th>Num Days</th><th>Area</th><th>MMEPool</th><th>Region</th><th>Vendor</th><th>VoLTE IA%</th><th>VoLTE LC%</th><th>SIP MOU</th><th>VoLTE QCI1 Setup Failure%</th><th>VoLTE QCI1 Setup Failures</th><th>VoLTE QCI1 Setup Attempts</th><th>QCI5 Setup Failure%</th><th>QCI5 Setup Failures</th><th>QCI5 Setup Attempts</th><th>SEER%</th><th>SEER SIP Ntwk Eff Calls</th><th>SEER SIP Call Attempts</th><th>RRC Setup Failure%</th><th>RRC Setup Attempts</th><th>RRC Setup Failures</th><th>VoLTE QCI1 Drop%</th><th>VoLTE QCI1 Drops</th><th>VoLTE QCI1 Established Calls</th><th>QCI5 Bearer Drop%</th><th>QCI5 Bearer Drops</th><th>QCI5 Bearer Estab Calls</th><th>VoLTE Avg MOS</th><th>VoLTE MOS Numerator</th><th>VoLTE MOS Denominator</th><th>VoLTE ACQ%</th><th>VoLTE Acceptable MOS Count</th><th>VoLTE Total MOS Count</th><th>VoLTE PDCP DL Volume MB</th><th>VoLTE DL Volume%</th><th>LTE PDCP DL Volume MB</th><th>VoLTE PDCP UL Volume MB</th><th>VoLTE UL Volume%</th><th>LTE PDCP UL Volume MB</th><th>SIP Call Drop%</th><th>SIP Call Drops</th><th>SIP Call Answers</th></tr>

Open in new window

0
 
HainKurtConnect With a Mentor Sr. System AnalystCommented:
ok, try to skip first tr, maybe th is treated as tr :) and making problems...
0
 
HainKurtSr. System AnalystCommented:
or on line 36, check if "cols.length > 0"

wrap line 36-77 with an if

if ($cols.length>0) {.... line 36-77 here ... }
0
 
brucegustPHP DeveloperAuthor Commented:
OK, Hain!

Here's what I did:

<?php
libxml_use_internal_errors(true);
if (file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html')) {
$datafile14 = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');
echo "Uploading Filename: https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html\n";
file_put_contents('raw_html/crystal.html',$datafile14);
//chmod("raw_html/crystal.html",0777);
echo "data has been uploaded!";
}
$html = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');


    /*** a new dom object ***/ 
    $dom = new domDocument; 

    /*** load the html into the object ***/ 
    $dom->loadHTML($html); 
	if(!$dom) 
	{
	echo "you've got a problem...!";
	}

    /*** discard white space ***/ 
    $dom->preserveWhiteSpace = false; 

    /*** the table by its tag name ***/ 
    $tables = $dom->getElementsByTagName('table'); 

    /*** get all rows from the table ***/ 
    $rows = $tables->item(0)->getElementsByTagName('tr'); 

    /*** loop over the table rows ***/ 
    foreach ($rows as $row) { 
        /*** get each column by tag name ***/ 
$cols = $row->getElementsByTagName('td'); 

if($cols)
{
$enddate = $cols->item(0)->nodeValue;
echo $enddate;
}
?>

Open in new window


...and here's what I got:

Uploading Filename: https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html data has been uploaded!
Notice: Trying to get property of non-object in /opt/lampp/htdocs/crystal/test.php on line 39
12/10/201412/10/201412/10/201412/10/201412/10/201412/10/2014
0
 
GaryCommented:
Use () not []

I've rejigged your code.

<?php
libxml_use_internal_errors(true);

$html = file_get_contents('test.html');


    /*** a new dom object ***/ 
    $dom = new domDocument; 

    /*** load the html into the object ***/ 
    $dom->loadHTML($html); 
	if(!$dom) 
	{
	echo "you've got a problem...!";
	}

    /*** discard white space ***/ 
    $dom->preserveWhiteSpace = false; 

    /*** the table by its tag name ***/ 
    $tables = $dom->getElementsByTagName('table')->item(0); 

    /*** get all rows from the table ***/ 

foreach($tables->getElementsByTagName('tr') as $row)
{
	$cols = $row->getElementsByTagName('td'); 

$enddate = $cols->item(0)->nodeValue;
$num_days = $cols->item(1)->nodeValue;
$area = $cols->item(2)->nodeValue;
$mmepool = $cols->item(3)->nodeValue;
$region = $cols->item(4)->nodeValue;
$vendor = $cols->item(5)->nodeValue;
$volte_ia_pcnt = $cols->item(6)->nodeValue;
$volte_lc_pcnt = $cols->item(7)->nodeValue;
$sip_mou = $cols->item(8)->nodeValue;
$volte_qci1_setup_failure_pcnt = $cols->item(9)->nodeValue;
$volte_qci1_setup_failures = $cols->item(10)->nodeValue;
$volte_qci1_setup_attempts = $cols->item(11)->nodeValue;
$qci5_setup_failure_pcnt = $cols->item(12)->nodeValue;
$qci5_setup_failures = $cols->item(13)->nodeValue;
$qci5_setup_attempts = $cols->item(14)->nodeValue;
$seer_pcnt = $cols->item(15)->nodeValue;
$seer_sip_ntwk_eff_calls = $cols->item(16)->nodeValue;
$seer_sip_call_attempts = $cols->item(17)->nodeValue;
$rrc_setup_failure_pcnt = $cols->item(18)->nodeValue;
$rrc_setup_attempts = $cols->item(19)->nodeValue;
$rrc_setup_failures = $cols->item(20)->nodeValue;
$volte_qci1_drop_pcnt = $cols->item(21)->nodeValue;
$volte_qci1_drops = $cols->item(22)->nodeValue;
$volte_qci1_established_calls = $cols->item(23)->nodeValue;
$qci5_bearer_drop_pcnt = $cols->item(24)->nodeValue;
$qci5_bearer_drops = $cols->item(25)->nodeValue;
$qci5_bearer_estab_calls = $cols->item(26)->nodeValue;
$volte_avg_mos = $cols->item(27)->nodeValue;
$volte_mos_numerator = $cols->item(28)->nodeValue;
$volte_mos_denominator = $cols->item(19)->nodeValue;
$volte_pdcp_dl_volume_mb = $cols->item(30)->nodeValue;
$volte_dl_volume_pcnt = $cols->item(31)->nodeValue;
$lte_pdcp_dl_volume_mb = $cols->item(32)->nodeValue;
$x7x24_volte_pdcp_dl_volume_mb = $cols->item(33)->nodeValue;
$x7x24_volte_dl_volume_pcnt = $cols->item(34)->nodeValue;
$x7x24_lte_pdcp_dl_volume_mb = $cols->item(35)->nodeValue;
$volte_pdcp_ul_volume_mb = $cols->item(36)->nodeValue;
$volte_ul_volume_pcnt = $cols->item(37)->nodeValue;
$lte_pdcp_ul_volume_mb = $cols->item(38)->nodeValue;
$x7x24_days = $cols->item(39)->nodeValue;
}
echo $x7x24_days;

 
?>

Open in new window

0
 
HainKurtSr. System AnalystCommented:
Notice: Trying to get property of non-object in /opt/lampp/htdocs/crystal/test.php on line 39
12/10/201412/10/201412/10/201412/10/201412/10/201412/10/2014

i see code is working... 6 date is printed... how many tr did you have here? more than 6 (excluding first th)?
if yes, whats the value in 7th tr? maybe last row should be skipped as well...

try

if ($cols.count>0) {...}

not sure what php uses :) may be len, length, size etc...
or maybe "count($cols)"... try, but I guess you know what the proper syntax is...
0
 
brucegustPHP DeveloperAuthor Commented:
Gary and Hain!

I used a hybrid of both your suggestions and here's the code I ran:

libxml_use_internal_errors(true);
if (file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html')) {
$datafile14 = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');
echo "Uploading Filename: https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html\n";
file_put_contents('raw_html/crystal.html',$datafile14);
//chmod("raw_html/crystal.html",0777);
echo "data has been uploaded!";
}
$html = file_get_contents('https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html');

    /*** a new dom object ***/ 
    $dom = new domDocument; 

    /*** load the html into the object ***/ 
    $dom->loadHTML($html); 
	if(!$dom) 
	{
	echo "you've got a problem...!";
	}

    /*** discard white space ***/ 
    $dom->preserveWhiteSpace = false; 

    /*** the table by its tag name ***/ 
    $tables = $dom->getElementsByTagName('table')->item(0); 

    /*** get all rows from the table ***/ 

foreach($tables->getElementsByTagName('tr') as $row)
{
	$cols = $row->getElementsByTagName('td'); 
	if(!$cols=="")
	{
	$x7x24_days = $cols->item(39)->nodeValue;
	}
}
echo $x7x24_days;

Open in new window


So, I get this on my page:

Uploading Filename: https://ncws.vh.eng.vzwcorp.com:8080/reports/delvane/volte_7_day.html data has been uploaded!
Notice: Trying to get property of non-object in /opt/lampp/htdocs/crystal/test.php on line 35
1.098

It tells me it's trying to get the property of a non object, and yet it prints the very object it says it can't access.

What is that? What am I doing wrong?
0
 
GaryCommented:
Can you attach the volte_7_day.html page
0
 
brucegustPHP DeveloperAuthor Commented:
Here you go...

volte.html
0
 
brucegustPHP DeveloperAuthor Commented:
Gary, that did it! Buy why?

Why when the id of "tr" is specified does the system still look process "th?"
0
 
GaryCommented:
Because you are specifically asking for the TD value when there is no TD's in the first row.
0
 
brucegustPHP DeveloperAuthor Commented:
Got it!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.