1

I am building a Twitter client w/ php and am receiving this error message when I try to run the program. It seems something to do with the server when I run it locally? I have php instlled. I am using Aptana as my IDE. What is the issue?

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = "hello";}
if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 20;}    
$getfield = '?screen_name=hello&count=20';
$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) {
    echo "Time and Date of Tweet: ".$items['created_at']."<br />";
    echo "Tweet: ". $items['text']."<br />";
    echo "Tweeted by: ". $items['user']['name']."<br />";
    echo "Screen name: ". $items['user']['screen_name']."<br />";
    echo "Followers: ". $items['user']['followers_count']."<br />";
    echo "Friends: ". $items['user']['friends_count']."<br />";
    echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
}
?>

error message:

Sorry, there was a problem.

Twitter returned the following error message:

".$string[errors][0]["message"]. "
";exit();} foreach($string as $items) { echo "Time and Date of Tweet: ".$items['created_at']."
"; echo "Tweet: ". $items['text']."
"; echo "Tweeted by: ". $items['user']['name']."
"; echo "Screen name: ". $items['user']['screen_name']."
"; echo "Followers: ". $items['user']['followers_count']."
"; echo "Friends: ". $items['user']['friends_count']."
"; echo "Listed: ". $items['user']['listed_count']."

error message in view source format:

<?php
require_once('TwitterAPIExchange.php");
$settings = array(
'oauth_access...    
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = "iagdotme";}
if (isset($_GET['count'])) {$count = $_GET['count'];} else {$count = 20;}
$getfield = '?screen_name=iagdotme&count=20';
$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)
    {
        echo "Time and Date of Tweet: ".$items['created_at']."<br />";
        echo "Tweet: ". $items['text']."<br />";
        echo "Tweeted by: ". $items['user']['name']."<br />";
        echo "Screen name: ". $items['user']['screen_name']."<br />";
        echo "Followers: ". $items['user']['followers_count']."<br />";
        echo "Friends: ". $items['user']['friends_count']."<br />";
        echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
    }
?>

there is a note that says: Saw "<?". Probable cause: Attempt to use an XML processing instruction in HTML. (XML processing instructions are not supported in HTML.)

here is the tutorial I am following: https://iag.me/socialmedia/build-your-first-twitter-app-using-php-in-8-easy-steps/

x01saa
  • 460
  • 1
  • 8
  • 18
user1925801
  • 154
  • 1
  • 9

3 Answers3

3

Based on your error message, your PHP code isn't executed at all. This is plain HTML output of your PHP code.

Your problem here might be either about configuring your web server (apache, nginx, ...) to interpret PHP, or adding a missing opening tag (<?php) before any PHP code.

fpietka
  • 1,027
  • 1
  • 10
  • 23
0

I use a different method to access Twitter's API but it looks to me like you may only be requesting user IDs and not user timeline. Change the URL so it looks like this:

$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';  
$getfield = '?screen_name=J7mbo';  
$requestMethod = 'GET';  

$twitter = new TwitterAPIExchange($settings);  
echo $twitter->setGetfield($getfield)  
    ->buildOauth($url, $requestMethod)  
    ->performRequest();
mkearney
  • 1,266
  • 10
  • 18
0

That PHP can run, I was able to run that on a server that I have access to. To do so all I needed was TwitterAPIExchange.php and the PHP you submitted. Having said that, I suggest you change

$string[errors][0]["message"]

to

$string["errors"][0]["message"]

so that it can return errors correctly and also change

$getfield = '?screen_name=iagdotme&count=20';

to (note the double quotes below, those are important)

$getfield = "?screen_name=$user&count=$count";

so that you actually use the preceding variables.

The problem probably lies with running the file on your PC; I suggest finding a web hosting provider.

ScegfOd
  • 402
  • 3
  • 12