Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on 

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?
PHP

Avatar of undefined
Last Comment
Bruce Gust
Avatar of Gary
Gary
Flag of Ireland image

Are you sure you are getting a webpage as that link errors out.
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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.

User generated image
Avatar of Gary
Gary
Flag of Ireland image

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?
Avatar of HainKurt
HainKurt
Flag of Canada image

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...
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

Yep! Exact same errors.
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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

SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of HainKurt
HainKurt
Flag of Canada image

or on line 36, check if "cols.length > 0"

wrap line 36-77 with an if

if ($cols.length>0) {.... line 36-77 here ... }
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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
Avatar of Gary
Gary
Flag of Ireland image

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

Avatar of HainKurt
HainKurt
Flag of Canada image

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...
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

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?
Avatar of Gary
Gary
Flag of Ireland image

Can you attach the volte_7_day.html page
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

Here you go...

volte.html
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

Gary, that did it! Buy why?

Why when the id of "tr" is specified does the system still look process "th?"
Avatar of Gary
Gary
Flag of Ireland image

Because you are specifically asking for the TD value when there is no TD's in the first row.
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

Got it!
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo