PHP错误空响应

时间:2017-04-28 16:15:50

标签: php twitter server cloud

下面的PHP代码应该可以工作,我从我的服务器收到错误,空响应。我试过关闭防火墙,但没有运气。你能帮助我吗?我一直在尝试不同的方法我不知道什么是错的。谢谢。

  <?php 
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' => ""

);


$url = "https://api.twitter.com/1.1/search/tweets.json";

$requestMethod = "GET";

$getfield = '?q=cloud%20computing&count=5&result_type=popular';


$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();


 ?>

1 个答案:

答案 0 :(得分:0)

  

你需要使用json_decode:http://php.net/manual/en/function.json-decode.php

<?php

error_reporting(E_ALL);
ini_set("display_errors", 1);

require_once('TwitterAPIExchange.php');

$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);

$url = "https://api.twitter.com/1.1/search/tweets.json";

$requestMethod = "GET";

$getfield = '?q=cloud%20computing&count=5&result_type=popular';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
         ->buildOauth($url, $requestMethod)
         ->performRequest();

$string = json_decode($response, true); // there you handle twitter response
print_r($string); // here is the echo'd result, not needed after

// from here, you have access to the data
// then, a foreach loop will give you access to whatever you want to echo

foreach($string['statuses'] as $tweets) {
$tweet = $tweets['text'];
echo "<p>Tweet: $tweet </p>";    
}
?>
  

使用循环示例

进行了更新