首页 > 解决方案 > Twitter 流 api 打印状态

问题描述

我有以下代码

<?php
/* 
This is an app to search tiwtter statuses.
*/


function queryTwitter($search) 
{
    $url = "https://api.twitter.com/1.1/search/tweets.json";
    if($search != "")
        $search = "#".$search;
    $query = array( 'count' => 100, 'q' => urlencode($search), "result_type" => "recent");
    $oauth_access_token = "XXXX";
    $oauth_access_token_secret = "xxxx";
    $consumer_key = "xxxx";
    $consumer_secret = "xxxx";

    $oauth = array(
                    'oauth_consumer_key' => $consumer_key,
                'oauth_nonce' => time(),
                'oauth_signature_method' => 'HMAC-SHA1',
                'oauth_token' => $oauth_access_token,
                'oauth_timestamp' => time(),
                'oauth_version' => '1.0');

$base_params = empty($query) ? $oauth : array_merge($query,$oauth);
$base_info = buildBaseString($url, 'GET', $base_params);
$url = empty($query) ? $url : $url . "?" . http_build_query($query);

$composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
$oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
$oauth['oauth_signature'] = $oauth_signature;

$header = array(buildAuthorizationHeader($oauth), 'Expect:');
$options = array( CURLOPT_HTTPHEADER => $header,
                  CURLOPT_HEADER => false,
                  CURLOPT_URL => $url,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_SSL_VERIFYPEER => false);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);
return  json_decode($json);
}

function buildBaseString($baseURI, $method, $params)
{
$r = array(); 
ksort($params);
foreach($params as $key=>$value){
    $r[] = "$key=" . rawurlencode($value); 
}
return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r)); 
}

function buildAuthorizationHeader($oauth)
{
$r = 'Authorization: OAuth '; 
$values = array(); 
foreach($oauth as $key=>$value)
    $values[] = "$key=\"" . rawurlencode($value) . "\""; 
$r .= implode(', ', $values); 
return $r; 
}




   // This is where I want to break down the object to an array and have it print out each individual tweet
   function displayTweets($object){

   $myArray = json_decode(json_encode($object), true);

   //print_r($myArray);

   foreach ($myArray as $tweet){

    print "Status: ";
    $array = print_r($tweet,true);
    print $array['text'];
    print "<br>";
  }


    

  }



  ?>

<html>
<head>

</head>

<body>

Search here for twitter statuses.

  <input type='text'> 

  <br>




  <?php

 $search = queryTwitter("dbz");

 //print_r($search);
 displayTweets($search);






  ?>


</body>


</html>

我正在尝试发布这样的状态... print $tweet['text'];

我不确定如何将 $search 对象转换为可以打印 $tweet['text'] 或打印 $tweet['location']; 的数组。

如何将函数 queryTwitter($search) 创建的对象转换为可打印数组。我还尝试 foreach 对象并打印出 $tweet->text 但它不起作用。当我使用 print_r($object) 时,它会打印出信息。如何完成 displayTweets 功能?

标签: phptwitter

解决方案


我想到了。

这是代码中的解决方案。

  function displayTweets($object){

    $myArray = json_decode(json_encode($object), true);
    //$myArray = json_encode($object);

    //echo print_r($myArray["statuses"][0]);

    foreach ($myArray["statuses"] as $tweet){

        echo "User :";
        echo $tweet['user']['screen_name'];
        echo "<br>";

        echo $tweet['text'];
       echo "<br>";
   }
}

推荐阅读