6

これが取引です。パスワードで保護された特定の Web ページの変更を監視し、ページのサイズが私が知っているものと異なる場合 (つまり、変更された場合) にサウンド アラームを再生する必要があります。これが私が思いついたコードの塊です。

<?php

$e = curl_init();
curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn');
curl_setopt($e, CURLOPT_POST, 1);
curl_setopt($e, CURLOPT_POSTFIELDS, 'UserName=xxx@example.com&Password=password');
curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($e, CURLOPT_REFERER, 'http://example.com');
curl_setopt($e, CURLOPT_RETURNTRANSFER, 1);
curl_exec($e);

$sizevalue = 2399;

do {
     curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php');
     $content = curl_exec($e);
     $numberofchars = strlen($content);
     sleep(15);
} while ($numberofchars = $sizevalue);

curl_close($e); 
echo '
      <audio controls="controls" autoplay="autoplay">
          <source src="/path/to/your/audio.mp3" type="audio/mp3">
          <source src="/path/to/your/audio.ogg" type="audio/ogg">
          <embed src="/path/to/your/audio.mp3">
      </audio>';
php?>

問題1。私が間違っている場合は修正してください。ただし、私が理解しているように、サーバーに接続する代わりに、ユーザー名とパスワードを1回だけ投稿し、接続を維持しcookie.txt、その後のアクションでその認証キーを使用して、ユーザー名とパスワードを常にログインフォームに送信します。周期の変わり目。これを修正するにはどうすればよいですか?

問題2。現時点で特定の条件が満たされない場合、基本的に無限ループになり、スクリプトをホストするサーバーがタイムアウトエラーを返すため、一時的なステータスを提供するスクリプトが必要です。

問題3。サウンドアラームを実装する最も簡単な方法は何ですか? OGGファイルの再生?ユーチューブのリダイレクト?

4

1 に答える 1

1

問題1curl_multi_execあなたの場合は本当に必要ありません。カール ハンドルを開いたままにしておくだけで十分です。

問題 2 :sleep()またはusleep()、この目的に適している

問題 3 : html マークアップをそのまま出力できた。もう 1 つのオプションは、アラーム音を自動的に再生するフラッシュ ファイルまたはビデオを埋め込むことです。これが実行されている場所と目的によって異なります。

このようなスクリプトは、ブラウザで実行するのが最適ではないことに注意してください。設定した間隔で URL をチェックし、do whileループやsleep.

これらの修正といくつかの例を使用して記述されたコードを次に示します。

<?php
set_time_limit(0); // this script will now run forever. but a safer value might be something like X hours or X number of minutes. 
$e = curl_init();
curl_setopt($e, CURLOPT_URL, 'http://example.com/Account/LogOn');
curl_setopt($e, CURLOPT_POST, true);
curl_setopt($e, CURLOPT_POSTFIELDS, 'UserName=xxx@example.com&Password=password');
// you'll need both CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE if you plan on NOT using curl's internal cookie handling
curl_setopt($e, CURLOPT_COOKIEJAR, 'cookie.txt'); // this is where we write the cookie data
curl_setopt($e, CURLOPT_COOKIEFILE, 'cookie.txt'); // this is where we read the cookie data to use
curl_setopt($e, CURLOPT_REFERER, 'http://example.com/');
curl_setopt($e, CURLOPT_RETURNTRANSFER, true);
// you can omit the next two lines if you think they won't be needed
curl_setopt($e, CURLOPT_FOLLOWLOCATION, true); // in case the remote server does some kind of http redirection, like a 301 redirect
curl_setopt($e, CURLOPT_MAXREDIRS, 3); // The maximum amount of HTTP redirections to follow, in case the remote server is badly configured and has an endless redirection loop
curl_exec($e);

$sizevalue = 2399;
$maximum_checks = 30; // I've added this as a cap for the loop, so that you only run it this many times.
$checks = 0;
$seconds = 15;

do {
     $checks++;
     curl_setopt($e, CURLOPT_URL, 'http://example.com/panel.php');
     $content = curl_exec($e);

     $numberofchars = strlen($content);

     // optionally... instead of the strlen method above you could also use 
     // http://php.net/manual/en/function.curl-getinfo.php
     if (!curl_errno($e)) {
        $info = curl_getinfo($e);
        echo 'content-length of download, read from Content-Length: field :' . $info['download_content_length']."<br>\n";
        $numberofchars = $info['download_content_length'];
     }
     if ($checks === $maximum_checks) {
        exit('No changes after '.($seconds * $maximum_checks).' seconds<br>'.PHP_EOL);
     }
     sleep($seconds); // wait for 15 seconds

} while ($numberofchars != $sizevalue); // fix up your TRUTH expression, don't forget it is != for proper comparison in your case. Without it you go into an infinite loop.

curl_close($e); 

// if our php script got this far then 
echo '
<audio controls="controls" autoplay="autoplay">
  <source src="/path/to/your/audio.mp3" type="audio/mp3">
  <source src="/path/to/your/audio.ogg" type="audio/ogg">
  <embed src="/path/to/your/audio.mp3">
</audio>';


?>
于 2012-12-11T17:23:58.920 に答える