duanrong3308 2014-02-11 19:19
浏览 40
已采纳

使用API​​ 1.1通过PHP检索特定的直接消息信息

I'm hoping you can help as I think I'm missing a trick here and can't find where I'm going wrong. I am looking to retrieve a few specific pieces of info from Twitter DMs via PHP. I've written some code to pull through what I'm after, but when running the script, only the message (tweet), ID, and date sent are pulling through. The rest like user name etc... remains blank. The current output can be found here: http://anonytine.hol.es/TDM94.php

Any help would be greatly appreciated:

  <?php
echo "<h2>Simple Twitter API Test</h2>";

ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php'); 


$settings = array(
'oauth_access_token' => "XXXX",
'oauth_access_token_secret' => "XXXX",
'consumer_key' => "XXXX",
'consumer_secret' => "XXXX"
);
$url = 'https://api.twitter.com/1.1/direct_messages.json';
$getfield = '?since_id=240136858829479935&count=20';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
         ->buildOauth($url, $requestMethod)
         ->performRequest(),$assoc = TRUE);

echo "<h3>Direct Messages</h3>";          
foreach($string as $items)
  {
    echo "Time and Date of Tweet: ".$items['created_at']."<br />";
    echo "Message ID: ".$items['id']."<br />";
    echo "Tweet: ".$items['text']."<br />";
    echo "Tweeted by: ". $items['user']['name']."<br />";
    echo "Screen name: ". $items['user']['screen_name']."<br />";
    echo "Location: ". $items['user']['geo_enabled']."<br />";
    echo "Friends: ". $items['user']['friends_count']."<br />";
    echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
  }
{

?>
  • 写回答

1条回答 默认 最新

  • dongnang8192 2014-02-11 19:28
    关注

    As per the doc here https://dev.twitter.com/docs/api/1.1/post/direct_messages/new you should

    be calling the following end point

    https://api.twitter.com/1.1/direct_messages/new.json

    The element names are different than what you are using.

    For the

    $items['user']['name']
    

    its

    $items['sender']['name']
    

    and so on

    In the new api call https://api.twitter.com/1.1/direct_messages.json

    check the returned array how data is returned before doing loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?