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 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。