Solved

How can I fix this error?

Posted on 2014-12-11
18
124 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
ID: 40494405
Are you sure you are getting a webpage as that link errors out.
0
 

Author Comment

by:brucegust
ID: 40494441
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
ID: 40494459
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 51

Expert Comment

by:HainKurt
ID: 40494521
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
ID: 40494534
Yep! Exact same errors.
0
 

Author Comment

by:brucegust
ID: 40494539
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
ID: 40494542
ok, try to skip first tr, maybe th is treated as tr :) and making problems...
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 40494546
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
ID: 40494591
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
 
LVL 58

Expert Comment

by:Gary
ID: 40494596
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
ID: 40494626
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
ID: 40494684
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
ID: 40494690
Can you attach the volte_7_day.html page
0
 

Author Comment

by:brucegust
ID: 40494707
Here you go...

volte.html
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 40494735
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
ID: 40494853
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
ID: 40494854
Because you are specifically asking for the TD value when there is no TD's in the first row.
0
 

Author Comment

by:brucegust
ID: 40494881
Got it!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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