duanlan5320 2015-06-11 14:05
浏览 1257

Twitter API - 获取推文有时无法获得推文

I've got a script that I use in PHP to write tweets to a text file and use them on my website. I've also set up a cron job that will hourly run the script and fetch the latest tweets. This works and is set up correctly.

However, I'm noticing that sometimes it fails to get any tweets and writes to the file leaving it as 0KB and thus I get my clients ringing me saying it's broken.

Has anyone else experience this problem with the twitter API?

Here's the script:

<?php
session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php");     
//Path to twitteroauth library
$twitteruser = "Username";
$notweets = 10;
$consumerkey = "****";
$consumersecret = "****";
$accesstoken = "*****";
$accesstokensecret = "*****";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
 $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
 return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

//Check twitter response for errors.
if ( isset( $tweets->errors[0]->code )) {
// If errors exist, print the first error for a simple notification.
echo "Error encountered: ".$tweets->errors[0]->message." Response code:" .$tweets->errors[0]->code;
} else {
// No errors exist. Write tweets to json/txt file.
$url = "/var/www/html/etc etc etc";
$file = $url.$twitteruser."-tweets.txt";
$fh = fopen($file, 'w') or die("can't open file");
fwrite($fh, json_encode($tweets));
fclose($fh);

if (file_exists($file)) {
   echo $file . " successfully written (" .round(filesize($file)/1024)."KB)";
} else {
    echo "Error encountered. File could not be written.";
}
}
?>
  • 写回答

1条回答 默认 最新

  • dongqiang5932 2015-06-11 14:23
    关注

    Your script is written to print the error message. You should investigate what that message was.

    If you're running it from cron, you can configure cron to to catch all output and send it in an email.

    You can also change the script to log the output, using error_log function or a logging library.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。