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.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本