Pregunta

I have created a application at dev.twitter.com and my application is posting data from my twitter in JSON right here: http://projweb.hj.se/~hada1192/twitter/tweets_json.php

I'm quite new with the Twitter API, JSON and PHP so my question now is: How can pick my tweets (I just want the text, for example "My first tweet", "my second tweet, twitter is fun", "Cant tell you how much how enjoy roller coasters" ) together in an PHP-array?

Here is my code:

<?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";
    }
 ?>
¿Fue útil?

Solución

The response is JSON and so you can use the built in php ( >= version 5.2) function json_decode() to decode the JSON into an associative array. From this you can access tweets something like the following:

$tweets = json_decode($response, true);

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

Otros consejos

The solution:

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>";
            }


        }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top