Parrotmaster
Parrotmaster

Reputation: 676

Handle errors file_get_contents

I'm writing an application that uses a .php script to get tweets using the twitter search API. See below code:

<?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);
?>

My problem is that I want to make sure that this script runs without fail, or if it does fail at least doesn't keep looping.

The script is going to be run automatically every 1 minute. Would anyone know a good way to handle errors here?

TL;DR: How do I handle errors on my code?

Upvotes: 0

Views: 135

Answers (1)

Dino Babu
Dino Babu

Reputation: 5809

in simple, use '@' prefix for function. It suppresses errors from displaying. Read More Here

<?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;
}
?>

Upvotes: 2

Related Questions