I am building a program that displays a twitter feed of a specific set of users using a common hashtag. The program gets an array of usernames and a hashtag from user input in a form. I am using a regular expression (something I'm not very good with) to weed out the tweets containing the hashtag, which I then want to echo out. However, the program is currently not functioning. Here's my work:
<?php
$hash = $_GET['hash'];
$users = $_GET['users'];
$users = str_replace(" ", "", $users);
$hash = str_replace(" ", "", $hash);
$users = explode(",",$users);
$regex = "/^[#]+[a-zA-Z0-9_]+[ ]$/";
$url = 'http://search.twitter.com/search.json?q='.$user.'&lang=en&rpp=100';
foreach($users as $user){
$jsontwitter = file_get_contents($url);
$twitter = json_decode($jsontwitter, true);
$twittertext = $twitter["results"];
if(preg_match($regex, $twittertext['text'])){
foreach($twittertext as $text){
echo '<a href="http://twitter.com/'.$text['from_user'].'">@'.$text['from_user'].'</a>: '.$text['text'].'<br>';
}
}
}
?>
Any suggestions?
You should first correct typos. Your q= variable is empty because the variable on line 8 is not defined above. It appears you may have wanted to use '$users' there, as defined on line 3. I would test from the command line (as shown below) with static variable set on line 2 and 3. Place print statements throughout to determine your fail point.
$ php test.html
Warning: file_get_contents(http://search.twitter.com/search.json?q=&lang=en&rpp=100): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
figured it out, with the help of a friend. the $url variable needed to be within the for loop in order to run the search with the defined $user variable. From there, the json is returned, decoded and the results are run through another for loop to isolate individual tweets that include the specified hashtag, using a regular expression in an if statement. the tweets are then echoed out. additionally, the way the regular expression was treated was incorrect - it was looking for a string that begins with a # and included any characters, ending with a space. It should be looking for a string with a # anywhere in it, that is followed by the $hash variable, followed by a space.
The resulting, working script:
<?php
$hash = $_GET['hash'];
$users = $_GET['users'];
$users = str_replace(" ", "", $users);
$hash = str_replace(" ", "", $hash);
$users = explode(",",$users);
$regex = "/($i)#". $hash ."/";
foreach($users as $user){
$url = 'http://search.twitter.com/search.json?q='.$user.'&lang=en&rpp=100';
$jsontwitter = file_get_contents($url);
$twitter = json_decode($jsontwitter, true);
$twittertext = $twitter["results"];
foreach($twittertext as $text){
if(preg_match($regex, $text['text'])){
echo '<a href="http://twitter.com/'.$text['from_user'].'">@'.$text['from_user'].'</a>: '.$text['text'].'<br>';
}
}
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.