【问题标题】:How to print Twitter API request reply?如何打印 Twitter API 请求回复?
【发布时间】:2014-09-04 14:59:03
【问题描述】:

我想打印回复中的所有推文。我错过了什么?

https://github.com/J7mbo/twitter-api-php

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

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);

/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

foreach ($response->statuses as $tweet) {
    echo $tweet->text;
}

注意:试图获取非对象的属性 /var/www/projects/twitter/twitter-api-php/index.php 在第 23 行

警告:为 foreach() 提供的参数无效 /var/www/projects/twitter/twitter-api-php/index.php 在第 23 行

【问题讨论】:

    标签: php twitter


    【解决方案1】:

    performRequest() 方法返回一个 JSON 字符串,而不是一个数组,所以你需要解码它$array = json_decode($response, true);

    【讨论】:

      【解决方案2】:

      你只需要使用:

      `echo $response;`
      

      或者改为:

      echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest();

      但我建议使用第一种方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-29
        • 2018-11-26
        • 2018-12-02
        • 2016-12-26
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 2021-01-21
        相关资源
        最近更新 更多