【问题标题】:How can I sort out the text from Twitter JSON API using php如何使用 php 从 Twitter JSON API 中整理出文本
【发布时间】:2014-05-22 15:39:54
【问题描述】:

我在 dev.twitter.com 创建了一个应用程序,我的应用程序正在这里以 JSON 格式从我的 Twitter 发布数据:http://projweb.hj.se/~hada1192/twitter/tweets_json.php

我对 Twitter API、JSON 和 PHP 很陌生,所以我现在的问题是:如何选择我的推文(我只想要文本,例如“我的第一条推文”、“我的第二条推文,推特很有趣", "不能告诉你过山车有多享受") 在一个 PHP 数组中?

这是我的代码:

<?php
require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth


// Use the data from http://dev.twitter.com/apps to fill out this info
// notice the slight name difference in the last two items)

    $connection = new tmhOAuth(array(
    'consumer_key' => 'xxx',
    'consumer_secret' => 'zz',
    'user_token' => '547733151-yyy', //access token
    'user_secret' => 'ooo' //access token secret
    ));

    // set up parameters to pass
    $parameters = array();

    if ($_GET['count']) {
    $parameters['count'] = strip_tags($_GET['count']);
    }

    if ($_GET['screen_name']) {
    $parameters['screen_name'] = strip_tags($_GET['screen_name']);
    }

    if ($_GET['twitter_path']) { $twitter_path = $_GET['twitter_path']; } else {
    $twitter_path = '1.1/statuses/user_timeline.json';
    }

    $http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );

    if ($http_code === 200) // if everything's good
    { 
        $response = strip_tags($connection->response['response']);

        if ($_GET['callback'])  // if we ask for a jsonp callback function
        {
            echo $_GET['callback'],'(', $response,');';
        } 
        else 
        {
            echo $response; 
        }
    }
    else 
    {
        echo "Error ID: ",$http_code, "<br>\n";
        echo "Error: ",$connection->response['error'], "<br>\n";
    }
 ?>

【问题讨论】:

    标签: php json twitter twitter-oauth


    【解决方案1】:

    响应是 JSON,因此您可以使用内置的 php (>= version 5.2) 函数 json_decode() 将 JSON 解码为关联数组。从这里您可以访问类似以下内容的推文:

    $tweets = json_decode($response, true);
    
    foreach( $tweets as $key => $tweet ) {
        echo $tweet["text"];
    }
    

    【讨论】:

    • if ($_GET['callback']) // 如果我们请求一个 jsonp 回调函数 { echo $_GET['callback'],'(', $response,');'; } 其他 { 回声 $response; /* $tweets = json_decode($response); foreach( $tweets as $key => $tweet ) { echo $tweet["text"]; } */ }
    • 没关系,我清除了它!谢谢你的帮助。你忘记了参数“true”
    【解决方案2】:

    解决办法:

    if ($_GET['callback'])  // if we ask for a jsonp callback function
            {
                echo $_GET['callback'],'(', $response,');';
            } 
            else 
            {
                //echo $response;   
    
                $tweets = json_decode($response, true);
    
                foreach( $tweets as $key => $tweet ) {
                    echo $tweet["text"] . "<br>";
                }
    
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-06-27
      • 2015-10-31
      • 1970-01-01
      • 2015-09-11
      • 2014-09-19
      • 2015-03-01
      • 2013-03-26
      • 2011-10-15
      • 2012-10-10
      相关资源
      最近更新 更多