0

.php スクリプトを使用して、twitter 検索 API を使用してツイートを取得するアプリケーションを作成しています。以下のコードを参照してください。

<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
// Local path
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/


$json = file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag. "%20-RT") or die("Could not get tweets");
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
?>

私の問題は、このスクリプトが確実に実行されるようにするか、失敗した場合でも少なくともループし続けないようにすることです。

スクリプトは 1 分ごとに自動的に実行されます。ここでエラーを処理する良い方法を知っている人はいますか?

TL;DR: コードのエラーを処理するにはどうすればよいですか?

4

1 に答える 1

2

簡単に言えば、関数に「@」プレフィックスを使用します。エラーの表示を抑制します。詳細はこちら

<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = @file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag . "%20-RT");
if (!empty($json)) {
  $fp = fopen($cacheFile, 'w');
  fwrite($fp, $json);
  fclose($fp);
} else {
  echo "Could not get tweets";
  exit;
}
?>
于 2013-04-11T11:56:08.137 に答える