2016-12-19 200 views
0

我正在嘗試製作一個小系統來幫助我工作。閱讀從這裏的一些文章,測試和失敗,我做了這個代碼工作:file_get_contents最後一個記錄

error_reporting(error_reporting() & ~E_NOTICE); 
header("Content-Type: text/html; charset=ISO-8859-1", true); 
$opts = array('http'=>array('method'=>"GET", 
      'header'=>"Accept-Language: pt-BR\r\n" . 
      "Cookie: ASPSESSIONIDSCSSRCRA=MHFGEDNDPHHBJDHGCMJKPDKN; ASPSESSIONIDQCSTTDRB=EKJNDDNDGGFMAHBFJABMJNAM".session_name()."=".session_id()."\r\n")); 
$context = stream_context_create($opts); 
session_write_close(); // unlock the file 
$url = "http://www.comprasnet.gov.br/pregao/fornec/mensagens_acomp.asp?prgcod=622924"; 
$contents = file_get_contents($url, false, $context); 
session_start(); // Lock the file 

echo ($contents); 

// Função para procura várias palavras em uma string 
function procpalavras01 ($contents, $palavras, $resultado = 0) { 
foreach ($palavras as $key => $value) { 
$pos = stripos($contents, $value); 
if ($pos !== false) { 
$palavras_encontradas[] = $value; } 
} 
if (is_array($palavras_encontradas)) { 
$palavras_encontradas = implode(",",$palavras_encontradas); 
} 
return $palavras_encontradas; 
} 

$palavras = array ("Ilma Chaves Pereira","19.026.964/0001-37","origina","correio","@"); 
$resultado = procpalavras01($contents, $palavras); 

//Variáveis 
$nome = ('ACLicita'); 
$mensagem = ('Pregoeiro chama no pregão'); 
$pregao = ('72016'); 
$uasg = ('160019'); 
$data_envio = date('d/m/Y'); 
$hora_envio = date('H:i:s'); 

// Compo E-mail 
$arquivo = " 
<style type='text/css'> 
body { 
margin:0px; 
font-family:Verdane; 
font-size:12px; 
color: #666666; 
} 
a{ 
color: #666666; 
text-decoration: none; 
} 
a:hover { 
color: #FF0000; 
text-decoration: none; 
} 
</style> 
<html> 
<table width='510' border='1' cellpadding='1' cellspacing='1' bgcolor='#CCCCCC'> 
     <tr> 
      <td> 
<tr> 
      <td width='500'>Nome:$nome</td> 
      </tr> 
      <tr> 
       <td width='320'>Mensagem:$mensagem</td> 
      </tr> 
      <tr> 
       <td width='320'>Pregão:$pregao</td> 
      </tr> 
      <tr> 
       <td width='320'>Uasg:$uasg</td> 
      </tr> 
      <tr> 
       <td width='320'>Palavra Encontrada:$resultado</td> 
      </tr> 
     </td> 
     </tr> 
     <tr> 
     <td>Este e-mail foi enviado em <b>$data_envio</b> às <b>$hora_envio</b></td> 
     </tr> 
    </table> 
</html> 
"; 
//enviar 

// emails para quem será enviado o formulário 
$emailenviar = "[email protected]"; 
$destino = $emailenviar; 
$assunto = "Atenção ao Pregão: " . $pregao . " Uasg: " . $uasg; 

// É necessário indicar que o formato do e-mail é html 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    $headers .= 'From: ACLicita'; 
//$headers .= "Bcc: $EmailPadrao\r\n"; 

if ($resultado === null) { 
} else { 
$enviaremail = mail($destino, $assunto, $arquivo, $headers); 
} 

它工作正常,但我想這是鏈接(按日期和時間)上只檢查的最後一條消息,如果已經發送,不要再發送。該網站有一個刷新系統,所以每次網站刷新時我的代碼都會刷新,如果找到$ palavras中的單詞,就會發送另一封郵件。幫助我或以某種方式展示我,我怎麼能改正這一點。我不知道我是否清楚自己。

編輯1:代碼試圖inserto到表:

if ($resultado === null) { 
} else { 
$enviaremail = mail($destino, $assunto, $arquivo, $headers); 
$sql = "INSERT INTO Licita (date, assunto, arquivo, email) 
VALUES ('$date', '$assunto', '$arquivo', '$emailenviar')"; 
} 
+0

我們怎麼能知道它已經發送或不? –

+0

那就是我想知道的一點。正如我之前所說,我不知道有關PHP的任何信息。我所知道的是,如果在鏈接中找到任何單詞,郵件就會到達。 –

+0

你說:「我希望這隻檢查鏈接上的最後一條消息(按日期和小時),如果它已經發送了,不要再發送郵件」,我們如何判斷它是否已發送? td中是否有特定字詞表示它已發送? –

回答

0

呦需要在一些地方保存像已發送哪些信息數據庫。在您的代碼中查看下面的評論。

//............................ 
if ($resultado === null) { 
} else { 
//get mail info from your local database (mysql) using current one 
//if(you get null then send email otherwise you already have sent email) 
$enviaremail = mail($destino, $assunto, $arquivo, $headers); 
//save this mail info to DB 
} 
//............................... 

實際上,要追蹤您發送了哪一個郵件,您需要在發送電子郵件後存儲每條記錄。在發送電子郵件之前,您需要與DB檢查數據庫中是否存在新的(最後一個)。
數據庫:每次發送郵件時,請將該郵件信息保存到本地存儲(文件,數據庫或任何建議的MySQl)。每次發送時,在發送前檢查當前記錄是否已發送。
Session:如果你只是想檢查最後一個,然後將該信息存儲到你的會話,並且不要讓你的會話過期。這樣你就會有通過電子郵件發送的最後一個記錄。
編輯:

  1. 將數據插入到數據庫:Insert
  2. 讀數據從DB:Read
+0

感謝您回覆Shailesh。我試了一下,開始一些mysql研究,因爲我不知道DB中的一個點。 –

+0

插入數據:http://www.w3schools.com/php/php_mysql_insert.asp 閱讀數據:http://www.w3schools.com/php/php_mysql_select.asp – webDev

+0

再次感謝,試圖在PHPmyadmin中創建一個數據庫,但需要閱讀更多關於類型,整理,int和其他需要的東西來創建一個數據庫。 –