Solved

How can I fix this error?

Posted on 2014-12-11
18
118 Views
Last Modified: 2014-12-11
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?
0
Comment
Question by:brucegust
  • 8
  • 6
  • 4
18 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Are you sure you are getting a webpage as that link errors out.
0
 

Author Comment

by:brucegust
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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
 

Author Comment

by:brucegust
Comment Utility
Yep! Exact same errors.
0
 

Author Comment

by:brucegust
Comment Utility
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
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
Comment Utility
ok, try to skip first tr, maybe th is treated as tr :) and making problems...
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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
 

Author Comment

by:brucegust
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
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
 

Author Comment

by:brucegust
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Can you attach the volte_7_day.html page
0
 

Author Comment

by:brucegust
Comment Utility
Here you go...

volte.html
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
Comment Utility
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
 

Author Comment

by:brucegust
Comment Utility
Gary, that did it! Buy why?

Why when the id of "tr" is specified does the system still look process "th?"
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Because you are specifically asking for the TD value when there is no TD's in the first row.
0
 

Author Comment

by:brucegust
Comment Utility
Got it!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now