Page 1 of 1

Why is this PHP code not working ?? help appreciated

Posted: Sun Dec 04, 2011 1:43 pm
by phpcoder85
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>";}
}



?>

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

Posted: Sun Dec 04, 2011 1:52 pm
by kc0pph
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.

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

Posted: Sun Dec 04, 2011 2:01 pm
by phpcoder85
I did read it ... and I am actually familiar with PHP but I dont seem to understand what is triggering the mistake

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

Posted: Sun Dec 04, 2011 2:24 pm
by kc0pph
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.

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

Posted: Sun Dec 04, 2011 2:30 pm
by phpcoder85
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

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

Posted: Sun Dec 04, 2011 2:32 pm
by kc0pph
Ok ill look at it, but ima be upset when its a missing curlie. ARRG lol.

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

Posted: Sun Dec 04, 2011 2:35 pm
by phpcoder85
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

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

Posted: Sun Dec 04, 2011 2:46 pm
by kc0pph

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?

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

Posted: Sun Dec 04, 2011 2:50 pm
by phpcoder85
Let me test it and I'll get back at ya !!!! I'll be right back , buddy :)

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

Posted: Sun Dec 04, 2011 2:57 pm
by phpcoder85
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 ??

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

Posted: Sun Dec 04, 2011 2:58 pm
by kc0pph

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.

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

Posted: Sun Dec 04, 2011 2:59 pm
by phpcoder85
I think it might be a missing closing curly brace .... but I can't seem to find it for some reason !!!! :(

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

Posted: Sun Dec 04, 2011 3:02 pm
by phpcoder85
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..

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

Posted: Sun Dec 04, 2011 3:03 pm
by phpcoder85
So what should I do on line 30 ... remove the opening CURLY brace after try ? that wouldnt make any sense ?

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

Posted: Sun Dec 04, 2011 3:10 pm
by phpcoder85
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.

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

Posted: Sun Dec 04, 2011 3:11 pm
by kc0pph
Found the culprit it was on a catch statement. Now getting another error. Let me work this out.

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

Posted: Sun Dec 04, 2011 3:13 pm
by phpcoder85
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 ?

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

Posted: Sun Dec 04, 2011 3:20 pm
by kc0pph
I havent changed anything (i think) since the last iteration i sent you. Im lost many maybe someone else can help you. Sorry.

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

Posted: Mon Dec 05, 2011 5:21 am
by egami
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.