Why is this PHP code not working ?? help appreciated

Links for php scripts

Moderators: macek, egami, gesf

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 1:43 pm

Ok the PHP code below generates this error and I got this code from book , I am totally confused ... The error warning is the following :
Parse error: syntax error, unexpected $end on line 85 ...85 is at the very bottom , what could possibly be wrong with my code ? Thanks a BUNCH !!!

The code is below :


<?php
include "twitter-async/EpiCurl.php";
include "twitter-async/EpiOAuth.php";
include "twitter-async/EpiTwitter.php";
define("TWITTER_CONSUMER_KEY","c7MyJzMMzHEKqknBlSVDA");
define("TWITTER_CONSUMER_SECRET","zwAtk4CRyUHz9USVAT41fA8SJbnIOhHHR1vJGaHCMU");
define("INDEX","first.php");
define("MASTER","master.php");
define("TITLE","test Tube - Sign In with Twitter");
function init($oauth_token= null, $oauth_token_secret= null){
return new EpiTwitter(TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET,$oauth_token,$oauth_token_secret);
}
function logout(){
$twitter = init($_COOKIE["oauth_token"], $_COOKIE["oauth_token_secret"]);
$twitter->post_accountEnd_session();
setcookie("oauth_token","",1);
setcookie("oauth_token_secret","",1);
header("Location: ".INDEX."?loggedout");
}
function login(){
//An Oauth Token has just been granted from Twitter
if(!empty($_GET["oauth_token"])){
$twitter = init();
$oauth_token = $_GET["oauth_token"];
try{
$twitter->setToken($oauth_token);
$token= $twitter->getAccessToken();
$twitter->setToken($token->oauth_token,$token->ouath_token_secret);
setcookie("oauth_token",$token->oauth_token);
setcookie("oauth_token_secret",$token->oauth_token_secret);
header("Location: ".MASTER."?loggedin");
}
catch(EpiOauthException $e) {header("Location:".INDEX."?oauthexception");}
catch(EpiTwitterException $e) {header("Location: ".INDEX."?exception");
}
} else if(
empty($_COOKIE["oauth_token"]) && empty($_COOKIE["oauth_token_secret"]))
{
setcookie("oauth_token","",1);
setcookie("oauth_token_secret","",1);
header("Location: ".INDEX);
} else{
return init($_COOKIE["oauth_token"],$_COOKIE["oauth_token_secret"]);
}
}
function verify($twitter){
if(is_object($twitter)) {
$response = $twitter->get_accountVerify_credentials();
return check($response);
} else{
return false;
}
function check($payload){
return ($payload->code == 200) ? $payload: false;
}

function printFriends($twitterObj, $count = 10) {
try {
$friends = $twitterObj->get_statusesFriends(array("cursor" => -1));
if (check($friends)) {
$next_cursor = $friends->next_cursor;
$previous_cursor = $friends->previous_cursor;
echo "<h2>Latest ".$count." Twitter Friends</h2>";
for ($i = 0; $i < $count; $i++) {
$friend = $friends->users[$i];
echo '<span><a title=\"'.$friend->name.'\" href="http://twitter.com/\"'.
$friend->screen_name.'\"><img class="following" src=\"'.$friend->
profile_image_url.'\" alt=\"'.$friend->screen_name.'\" height="48"
width="48" /></a></span>';
}
} else {
return false;
}
}


catch(EpiTwitterException $e) {echo "<p> You have no friends to list .</p>";}
}



?>

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 1:52 pm

you are missing a } somewhere.

www.php.net

read the WHOLE manual and then start coding. Its the best thing you can do. Typing things out of a book and hoping they work is not a good strategy.

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:01 pm

I did read it ... and I am actually familiar with PHP but I dont seem to understand what is triggering the mistake

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 2:24 pm

Check all of your {

I just put the code in Dreamweaver and its showing that it is missing a curly where i have no idea, not a big fan of counting 100's of curlies.

Since your famaliar with PHP you should understand this but i will point it out for reference.
all { have to have a matching }

Verify that and if it did not solve your problem ill take a look at it more in depth.

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:30 pm

Thanks Kcopph , but I have checked again and again , the opening curly braces have their matching closing ones ... I would really appreciate if you helped me understand where this error comes from... Thanks a bunch

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 2:32 pm

Ok ill look at it, but ima be upset when its a missing curlie. ARRG lol.

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:35 pm

I'd love to keep in touch with you as a programmer buddy when you fix this .. I am actually coding a big social network from SCRATCH !!!! nothing like facebook , the idea is totally different ....Please help!! :D

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 2:46 pm

Code: Select all

<?php
include "twitter-async/EpiCurl.php";
include "twitter-async/EpiOAuth.php";
include "twitter-async/EpiTwitter.php";
define("TWITTER_CONSUMER_KEY","c7MyJzMMzHEKqknBlSVDA");
define("TWITTER_CONSUMER_SECRET","zwAtk4CRyUHz9USVAT41fA8SJbnIOhHHR1vJGaHCMU");
define("INDEX","first.php");
define("MASTER","master.php");
define("TITLE","test Tube - Sign In with Twitter");
function init($oauth_token= null, $oauth_token_secret= null)
{
   return new EpiTwitter(TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET,$oauth_token,$oauth_token_secret);
}
function logout()
{
   $twitter = init($_COOKIE["oauth_token"], $_COOKIE["oauth_token_secret"]);
   $twitter->post_accountEnd_session();
   setcookie("oauth_token","",1);
   setcookie("oauth_token_secret","",1);
   header("Location: ".INDEX."?loggedout");
}
function login()
{
   //An Oauth Token has just been granted from Twitter
   if(!empty($_GET["oauth_token"]))
   {
      $twitter = init();
      $oauth_token = $_GET["oauth_token"];
      try
      {
         $twitter->setToken($oauth_token);
         $token= $twitter->getAccessToken();
         $twitter->setToken($token->oauth_token,$token->ouath_token_secret);
         setcookie("oauth_token",$token->oauth_token);
         setcookie("oauth_token_secret",$token->oauth_token_secret);
         header("Location: ".MASTER."?loggedin");
      }
   catch(EpiOauthException $e) {header("Location:".INDEX."?oauthexception");}
   catch(EpiTwitterException $e) {header("Location: ".INDEX."?exception");
   }
}
elseif(empty($_COOKIE["oauth_token"]) && empty($_COOKIE["oauth_token_secret"]))
{
   setcookie("oauth_token","",1);
   setcookie("oauth_token_secret","",1);
   header("Location: ".INDEX);
}
else
{
   return init($_COOKIE["oauth_token"],$_COOKIE["oauth_token_secret"]);
}
}
function verify($twitter)
{
   if(is_object($twitter))
   {
      $response = $twitter->get_accountVerify_credentials();
      return check($response);
   }
   else
   {
      return false;
   }
function check($payload)
{
   return ($payload->code == 200) ? $payload: false;
}

function printFriends($twitterObj, $count = 10)
{
   try
   {
      $friends = $twitterObj->get_statusesFriends(array("cursor" => -1));
      if (check($friends))
      {
         $next_cursor = $friends->next_cursor;
         $previous_cursor = $friends->previous_cursor;
         echo "<h2>Latest ".$count." Twitter Friends</h2>";
         for ($i = 0; $i < $count; $i++)
         {
            $friend = $friends->users[$i];
            echo '<span><a title=\"'.$friend->name.'\" href="http://twitter.com/\"'.
            $friend->screen_name.'\"><img class="following" src=\"'.$friend->
            profile_image_url.'\" alt=\"'.$friend->screen_name.'\" height="48"
            width="48" /></a></span>';
         }
      }
      else
      {
      return false;
      }
   }


   catch(EpiTwitterException $e) {echo "<p> You have no friends to list .</p>";}
}



?>


On Line 42 of the Neat and Clean and Readable code you have an elseif. There is nothing for an else, you need an if statement above it. Maybe its supposed to be if. That could throw an error but not sure if it throws an $end error.
Below is an else statement is the elseif ment to be an if?

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:50 pm

Let me test it and I'll get back at ya !!!! I'll be right back , buddy :)

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:57 pm

I got that code from a book that actually teaches how to implement twitter applications to your website ... do you want me to change the else to an if ??

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 2:58 pm

Code: Select all

<?php
include "twitter-async/EpiCurl.php";
include "twitter-async/EpiOAuth.php";
include "twitter-async/EpiTwitter.php";
define("TWITTER_CONSUMER_KEY","c7MyJzMMzHEKqknBlSVDA");
define("TWITTER_CONSUMER_SECRET","zwAtk4CRyUHz9USVAT41fA8SJbnIOhHHR1vJGaHCMU");
define("INDEX","first.php");
define("MASTER","master.php");
define("TITLE","test Tube - Sign In with Twitter");
function init($oauth_token= null, $oauth_token_secret= null)
{
   return new EpiTwitter(TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET,$oauth_token,$oauth_token_secret);
}
function logout()
{
   $twitter = init($_COOKIE["oauth_token"], $_COOKIE["oauth_token_secret"]);
   $twitter->post_accountEnd_session();
   setcookie("oauth_token","",1);
   setcookie("oauth_token_secret","",1);
   header("Location: ".INDEX."?loggedout");
}
function login()
{
   //An Oauth Token has just been granted from Twitter
   if(!empty($_GET["oauth_token"]))
   {
      $twitter = init();
      $oauth_token = $_GET["oauth_token"];
      try
      {
         $twitter->setToken($oauth_token);
         $token= $twitter->getAccessToken();
         $twitter->setToken($token->oauth_token,$token->ouath_token_secret);
         setcookie("oauth_token",$token->oauth_token);
         setcookie("oauth_token_secret",$token->oauth_token_secret);
         header("Location: ".MASTER."?loggedin");
      }
      catch(EpiOauthException $e) {header("Location:".INDEX."?oauthexception");}
      catch(EpiTwitterException $e) {header("Location: ".INDEX."?exception");
   }
}
if(empty($_COOKIE["oauth_token"]) && empty($_COOKIE["oauth_token_secret"]))
{
   setcookie("oauth_token","",1);
   setcookie("oauth_token_secret","",1);
   header("Location: ".INDEX);
}
else
{
   return init($_COOKIE["oauth_token"],$_COOKIE["oauth_token_secret"]);
}
//}
function verify($twitter)
{
   if(is_object($twitter))
   {
      $response = $twitter->get_accountVerify_credentials();
      return check($response);
   }
   else
   {
      return false;
   }
}
function check($payload)
{
   return ($payload->code == 200) ? $payload: false;
}

function printFriends($twitterObj, $count = 10)
{
   try
   {
      $friends = $twitterObj->get_statusesFriends(array("cursor" => -1));
      if (check($friends))
      {
         $next_cursor = $friends->next_cursor;
         $previous_cursor = $friends->previous_cursor;
         echo "<h2>Latest ".$count." Twitter Friends</h2>";
         for ($i = 0; $i < $count; $i++)
         {
            $friend = $friends->users[$i];
            echo '<span><a title=\"'.$friend->name.'\" href="http://twitter.com/\"'.
            $friend->screen_name.'\"><img class="following" src=\"'.$friend->
            profile_image_url.'\" alt=\"'.$friend->screen_name.'\" height="48"
            width="48" /></a></span>';
         }
      }
      else
      {
      return false;
      }
   }


   catch(EpiTwitterException $e) {echo "<p> You have no friends to list .</p>";}
}



?>


Getting an error unexpected { on line 30 Not sure what that is, I am a novice programmer myself. Dont think there would be an issue with a try statement inside of an if statement.
Im going to pass the buck on this one. I removed 1 curly brace and placed 1 in a different part (told you it was them) and then made it neat so it could be debugged fast. The error is on line 30 with the Try statement.

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 2:59 pm

I think it might be a missing closing curly brace .... but I can't seem to find it for some reason !!!! :(

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 3:02 pm

Guess what ... While trying control F to count the opening and closing curly braces , there is 19 opening curly braces , but only 18 closing ones .. so there is ONE missing for sure but I can not find it ..lol .. This is going to make me shoot myself ...lol..

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 3:03 pm

So what should I do on line 30 ... remove the opening CURLY brace after try ? that wouldnt make any sense ?

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 3:10 pm

phpcoder85 wrote:I think it might be a missing closing curly brace .... but I can't seem to find it for some reason !!!! :(


How would it work if I removed the brace after the try , I mean that's how it's code by the rules of the PHP language .. please dont give up .. Your help will really be appreciated. This has been my biggest challenge so far , everything else has been a breeze.

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 3:11 pm

Found the culprit it was on a catch statement. Now getting another error. Let me work this out.

phpcoder85
New php-forum User
New php-forum User
Posts: 13
Joined: Sun Dec 04, 2011 10:43 am

Re: Why is this PHP code not working ?? help appreciated

Postby phpcoder85 » Sun Dec 04, 2011 3:13 pm

what catch statement ? THANKS A BUNCH for helping out ... Geez, I love PROGRAMMING to death but getting stuck on debuggging sometimes can drive you nuts .. what other error is there ?

kc0pph
New php-forum User
New php-forum User
Posts: 86
Joined: Sat Nov 26, 2011 8:39 am
Location: Pueblo, CO
Contact:

Re: Why is this PHP code not working ?? help appreciated

Postby kc0pph » Sun Dec 04, 2011 3:20 pm

I havent changed anything (i think) since the last iteration i sent you. Im lost many maybe someone else can help you. Sorry.

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Why is this PHP code not working ?? help appreciated

Postby egami » Mon Dec 05, 2011 5:21 am

In a perfect debug world.. you have the /var/log/http/error_log rolling on one screen.. and echo "catch me" on routines and functions.

remove your script, and add things one at a time, with catch-me's after each one and then you can see where it's failing.


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest

cron