尝试使用PHP在Twitter上运行搜索并输出结果


Trying to run a search on Twitter using PHP and output the results

好的,所以我正在运行一些使用Twitter API与PHP的测试。

在"很多"混乱之后,我让它工作了…的

然而,我有嵌套数组的问题,起初我可以读回任何信息,但后来意识到我所追求的是数组中的数组。

我应该指出(尽管当您查看代码时,它可能非常明显)。我不是一个PHP专家,只是一个得到足够的人能够把东西分开并定制它们,这就是我在下面所做的。

<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "SECRET",
    'oauth_access_token_secret' => "SECRET",
    'consumer_key' => "SECRET",
    'consumer_secret' => "SECRET"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';

$requestMethod = "GET";
$getfield = '?q=%22anyone%20recommend%22&result_type=recent';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string as $items)
    {
    foreach ($items as $value){ 
        echo "Time and Date of Tweet: ".$value['created_at']."<br />";
        echo "Tweet: ". $value['text']."<br />";
        echo "Tweeted by: ". $value['user']['name']."<br />";
        echo "Screen name: ". $value['user']['screen_name']."<br />";
        echo "Followers: ". $value['user']['followers_count']."<br />";
        echo "Friends: ". $value['user']['friends_count']."<br />";
        echo "Listed: ". $value['user']['listed_count']."<br /><hr />";
        }
    }
?>

这是我得到的结果,这是正确的,直到最后3或4个响应:

Time and Date of Tweet: Tue Sep 13 14:27:00 +0000 2016
Tweet: Can anyone recommend a good thing to have
Tweeted by: jmprcx
Screen name: jmprcx
Followers: 1194
Friends: 5
Listed: 2
Time and Date of Tweet: Tue Sep 13 14:25:49 +0000 2016
Tweet: RT @SunSparklesBlog: 1/? Really want to have a new layout for my blog. I have some ideas what I want, can anyone recommend a blog design co…
Tweeted by: SpainScope
Screen name: SpainScope
Followers: 8060
Friends: 7129
Listed: 2436
Time and Date of Tweet: Tue Sep 13 14:25:17 +0000 2016
Tweet: #bbloggers Can anyone recommend a nail treatment for weak nails after gel/acryilics please? Not OPI nail envy as that didn't work for me 🙂
Tweeted by: Zoë
Screen name: MammafulZo
Followers: 3080
Friends: 309
Listed: 103
Time and Date of Tweet: Tue Sep 13 14:24:44 +0000 2016
Tweet: Can anyone recommend a good setting spray? I'm going to a wedding this weekend and I need my makeup to last lol
Tweeted by: Kayla✨
Screen name: xdeadweightx_
Followers: 948
Friends: 1392
Listed: 1
Time and Date of Tweet: Tue Sep 13 14:23:27 +0000 2016
Tweet: RT @Merlinho_: Can anyone recommend a #book or book series for 7-8 year old girls please? RT appreciated. Thanks X
Tweeted by: Del The Chauffeur
Screen name: DJWalker11
Followers: 2323
Friends: 2303
Listed: 77
Time and Date of Tweet: Tue Sep 13 14:22:36 +0000 2016
Tweet: RT @Merlinho_: Can anyone recommend a #book or book series for 7-8 year old girls please? RT appreciated. Thanks X
Tweeted by: Del
Screen name: DE180Y
Followers: 806
Friends: 361
Listed: 22
Time and Date of Tweet: Tue Sep 13 14:22:31 +0000 2016
Tweet: Tomorrow I shall be exploring Jesmond, Newcastle for good coffee shops/health restaurants. Can anyone recommend? #NEBlogger #NEFollowers
Tweeted by: Ellie Travis
Screen name: Perf_STherapy
Followers: 764
Friends: 2173
Listed: 41
Time and Date of Tweet: Tue Sep 13 14:21:58 +0000 2016
Tweet: RT @Merlinho_: Can anyone recommend a #book or book series for 7-8 year old girls please? RT appreciated. Thanks X
Tweeted by: BuxtonGooner
Screen name: buxtongooner
Followers: 2783
Friends: 805
Listed: 78
Time and Date of Tweet: Tue Sep 13 14:21:18 +0000 2016
Tweet: can anyone recommend co-working spaces in San Diego? or does anyone have an extra desk at their office that I can pull up to?
Tweeted by: Andrew M. Galvan
Screen name: andrewmgalvan
Followers: 913
Friends: 1214
Listed: 16
Time and Date of Tweet: Tue Sep 13 14:21:02 +0000 2016
Tweet: My belly is expanding and I can't fit into my jeans anymore 󾌡 Can anyone recommend somewhere I can get maternity... 
Tweeted by: Kristen O'Gorman
Screen name: KOGaroo
Followers: 315
Friends: 778
Listed: 9
Time and Date of Tweet: Tue Sep 13 14:20:19 +0000 2016
Tweet: RT @Merlinho_: Can anyone recommend a #book or book series for 7-8 year old girls please? RT appreciated. Thanks X
Tweeted by: Nick Davison
Screen name: njd10happy
Followers: 3156
Friends: 3044
Listed: 40
Time and Date of Tweet: Tue Sep 13 14:19:37 +0000 2016
Tweet: Can anyone recommend a #book or book series for 7-8 year old girls please? RT appreciated. Thanks X
Tweeted by: Merlinho
Screen name: Merlinho_
Followers: 1842
Friends: 811
Listed: 43
Time and Date of Tweet: Tue Sep 13 14:19:29 +0000 2016
Tweet: can anyone recommend me any Graphics Tablets? dunno what ones to get
Tweeted by: jodinski
Screen name: JoDinski
Followers: 2788
Friends: 325
Listed: 17
Time and Date of Tweet: Tue Sep 13 14:19:05 +0000 2016
Tweet: SAGINAW PEOPLE HELP ME OUT!!!!! Can anyone recommend a good daycare center here in the area. Preferably a... 
Tweeted by: Daniel A.A. Ishmel
Screen name: DanielsAMG2016
Followers: 49
Friends: 223
Listed: 0
Time and Date of Tweet: Tue Sep 13 14:17:01 +0000 2016
Tweet: Can anyone recommend a nice tinted moisturiser or bb cream i cannot deal with foundation anymore and my complexion is shite x
Tweeted by: Shannon
Screen name: watsonehtelly
Followers: 377
Friends: 284
Listed: 2
Time and Date of Tweet:
Tweet:
Tweeted by:
Screen name:
Followers:
Friends:
Listed:
Time and Date of Tweet:
Tweet:
Tweeted by:
Screen name:
Followers:
Friends:
Listed:
Time and Date of Tweet: 7
Tweet: 7
Fatal error: Cannot use string offset as an array in 
/home/public_html/bots/twitter3.php on line 31

注意最后两个响应是空的,然后是错误。

我使用TwitterAPIExchange.php从这里:https://github.com/J7mbo/twitter-api-php

我遵循这个指南:http://iag.me/socialmedia/build-your-first-twitter-app-using-php-in-8-easy-steps/

谁能告诉我哪里出错了?

显然,你得到一个点,$value['created_at']不存在了。要分析这种情况,您需要以这种方式探索$string的内容:

echo "'n<p>DEBUG: string = <pre>" . print_r($string) . "</pre>";

一旦你在屏幕上得到这个变量转储,你将能够找出为什么$value缺少'created_at'键并修复它。