我正在创建一个在我的数据库中下载邮件数据的功能。这是我的代码:
include('simple_html_dom.php');
$html = new simple_html_dom();
global $html;
function getArraySubject($stream,$subject){
$array = imap_search($stream, $subject);
return $array ;
}
// $domain 0 = any domeain from which we are receiving mails
//$case = subject number for ex :- Query, Potential.....
function getDataFromHTML($subjectArray, $domain = 0, $case = 1)
{
$completeArray = array();
if ($domain == 0 && $case == 1) {
rsort($subjectArray);
foreach($subjectArray as $email_id){
$body = imap_qprint(imap_body($stream,$email_id));
$my_file = 'mail-data.txt';
file_put_contents($my_file, $body);
$html = file_get_contents($my_file, true);
$htmlData = $html->str_get_html($body);
$tds = $htmlData->find('table',3)->find('td');
$num = null;
$i = 0 ;
foreach($tds as $td){
$completeArray['magicbricks']['case1'][$i] = $td->innertext;
$i++;
}
}
}
return $completeArray;
}
我收到错误"致命错误:在行号中的非对象上调用成员函数str_get_html()。 25"
如何解决此问题?请帮忙。
这是我的第二个文件,我在上面提到上面提到的函数。
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('functions.php');
// Configure your imap mailboxes
$mailboxes = array(
array(
'label' => 'Label',
'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX',
'username' => 'abc@uabc.com',
'password' => 'abc246'
)
);
foreach ($mailboxes as $current_mailbox) {
// Open an IMAP stream to our mailbox
$stream = @imap_open($current_mailbox['mailbox'], $current_mailbox['username'], $current_mailbox['password']);
if (!$stream) {
?>
<p>Could not connect to: <?php echo $current_mailbox['label']?>. Error: <?php echo imap_last_error()?></p>
<?php
} else {
$sub1 = 'SUBJECT "Query" FROM "magicbricks.com"';
$array1 = getArraySubject($stream,$sub1);
print_r($array1);
if (!count($array1)){
?>
<p>No e-mails found.</p>
<?php
} else {
$result = getDataFromHTML($array1, $domain = 0, $case = 1);
print_r($result);
}
}
// Close our imap stream.
imap_close($stream);
} // end foreach
答案 0 :(得分:2)
在此上下文中,$html
很可能是一个字符串,因此您无法附加任何方法:
$html = file_get_contents($my_file, true); // this is not an instance of simple-html-dom object
$htmlData = $html->str_get_html($body);
^
删除:
$htmlData = str_get_html($body); // now this create a simple-html-dom object
答案 1 :(得分:0)
include('simple_html_dom.php');
$html = new simple_html_dom();
global $html; //This is unnecessary
function getArraySubject($stream,$subject){
$array = imap_search($stream, $subject);
return $array ;
}
// $domain 0 = any domeain from which we are receiving mails
//$case = subject number for ex :- Query, Potential.....
function getDataFromHTML($subjectArray, $domain = 0, $case = 1)
{
//Add this
global $html;
$completeArray = array();
if ($domain == 0 && $case == 1) {
rsort($subjectArray);
foreach($subjectArray as $email_id){
$body = imap_qprint(imap_body($stream,$email_id));
$my_file = 'mail-data.txt';
file_put_contents($my_file, $body);
$fileData = file_get_contents($my_file, true); //renamed this
$htmlData = $html->str_get_html($body);
$tds = $htmlData->find('table',3)->find('td');
$num = null;
$i = 0 ;
foreach($tds as $td){
$completeArray['magicbricks']['case1'][$i] = $td->innertext;
$i++;
}
}
}
return $completeArray;
}