Need Help IN PHP to contact twitter API

Discussion in 'PHP' started by jss4ever, Oct 23, 2009.

  1. #1
    Hey

    I Have a site with a script purchased from twitterscripts.net

    my site is www. tweetwonder .com

    the script functions to contact twitter and update account statuses

    but the script is not working and not updating , it's not a coding error :(

    it might be sumthin needed to be setup as SOAP or Something

    i have 0 % knowledge in PHP


    the site is hosted at godaddy

    PHP INFO

    tweetwonder.com/php.php

    Here is the code:

    
    
    
    class twitter {
    	
    	private $user;
    	private $pass;
    	private $ch;
    	private $twitterHost = "http://twitter.com/";
    	
    	public function __construct($username, $passwd) {
    		$this->user = $username;
    		$this->pass = $passwd;
    		
    		/* Create and setup  the curl Session */
    		$this->ch = curl_init();
    	
    		curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
    		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($this->ch, CURLOPT_USERPWD, "$this->user:$this->pass");
    		curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    		curl_setopt($this->ch, CURLOPT_POST, 1);
    	}
    	
    	public function __destruct() {
    		/*clean Up */
    		curl_close($this->ch);
    	}
    
    	public function setStatus($stat) {
    	
    		if(strlen($stat) < 1)
    			return false; 
    		
    		/*Set the host information for the curl session */
    		$this->twitterHost .= "statuses/update.xml?status=". urlencode(stripslashes(urldecode($stat)));
    		
    		curl_setopt($this->ch, CURLOPT_URL, $this->twitterHost);
    		
    		/* Execute and get the http code to see if it was succesfull or not*/
    		$result = curl_exec($this->ch);	
    		$resultArray = curl_getinfo($this->ch);
    				
    		if ($resultArray['http_code'] == 200) ;
    			return true;
    			
    		return false;
    	}
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <title>&lt;?php echo &quot;$name&quot;;?&gt;</title>
    <meta content="application/xhtml+xml; charset=iso-8859-1" http-equiv="content-type" />
    <meta content="" name="description" />
    <meta content="" name="keywords" />
    <link href="bluestyle210.css" media="screen,projection" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    
    <div id="container">
    	<div id="outcome">
    		<?php
    
    
    
    $curTwitter = new twitter("$user", "$pass");
    
    if (isset($_POST['submit'])) {
    
    	$twitter_status = $_POST['twitter_stat'];
    	
    	if (strlen($twitter_status) > 0) {
    
    		if( $curTwitter->setStatus($twitter_status) == true)
    			echo "<p>Updated Succesfully</p>";
    		else
    			echo "<p>Twitter is unavailable at this time</p>";
    	} else
    		echo "<p>Error: I won't send blank messages!</p>";
    
    
    }
    
    ?></div>
    	<div id="main">
    		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    			<p>Username &amp; Password:</p>
    			<input id="user" class="user" name="user" type="text">
    			<input id="pass" class="pass" name="pass" type="password">
    			<p>140 Character Message:</p>
    			<textarea id="twitter_stat" maxlength="140" name="twitter_stat" type="text"></textarea>
    			<input id="submit" name="submit" type="submit" value="Send" /><br />
    			<a href="http://<?php echo "$url";?>"><?php echo "$site";?></a>
    		</form>
    		<div class="clearer">
    		</div>
    	</div>
    </div>
    
    </body>
    
    </html>
    
    
    Code (markup):


    add me on msn -- offers @ jssnetwork.com --

    if anyone's able to done this i will provide a free web-design service + coding

    you may find that post useful

    http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22888188.html


    Please Help me ASAP as i need to start that website


    thanx in advance
     
    Last edited: Oct 23, 2009
    jss4ever, Oct 23, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well for starters i don't know if maybe you just forgot to include it here, but there's no <?php at the start of your code (there should be!). Second, your POST is incorrect - let me fix it in a sec.

    <?php
    	
    class twitter {
    	
    	private $user;
    	private $pass;
    	private $ch;
    	private $twitterHost = "http://twitter.com/";
    	
    	public function __construct() {
    		$this->user = trim($_POST['user']);
    		$this->pass = trim($_POST['pass']);
    		
    		/* Create and setup  the curl Session */
    		$this->ch = curl_init();
    	
    		curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
    		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
    		curl_setopt($this->ch, CURLOPT_USERPWD, "{$this->user}:{$this->pass}");
    		curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    		curl_setopt($this->ch, CURLOPT_POST, 1);
    	}
    	
    	public function __destruct() {
    		/*clean Up */
    		curl_close($this->ch);
    	}
    
    	public function setStatus($stat) {
    	
    		if(strlen($stat) < 1)
    			return false; 
    		
    		/*Set the host information for the curl session */
    		$this->twitterHost .= "statuses/update.xml";
    
    		curl_setopt($this->ch, CURLOPT_URL, $this->twitterHost);
    
    		curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query(array("status" => stripslashes(urldecode($stat)))));
    
    		
    		/* Execute and get the http code to see if it was succesfull or not*/
    		$result = curl_exec($this->ch);
    		$resultArray = curl_getinfo($this->ch);
    				
    		if ($resultArray['http_code'] == 200) ;
    			return true;
    			
    		return false;
    	}
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <title>&lt;?php echo &quot;$name&quot;;?&gt;</title>
    <meta content="application/xhtml+xml; charset=iso-8859-1" http-equiv="content-type" />
    <meta content="" name="description" />
    <meta content="" name="keywords" />
    <link href="bluestyle210.css" media="screen,projection" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    
    <div id="container">
    	<div id="outcome">
    		<?php
    
    
    
    $curTwitter = new twitter();
    		
    if (isset($_POST['submit'])) {
    
    	$twitter_status = $_POST['twitter_stat'];
    	
    	if (strlen($twitter_status) > 0) {
    
    		if( $curTwitter->setStatus($twitter_status) == true)
    			echo "<p>Updated Succesfully</p>";
    		else
    			echo "<p>Twitter is unavailable at this time</p>";
    	} else
    		echo "<p>Error: I won't send blank messages!</p>";
    
    
    }
    
    ?></div>
    	<div id="main">
    		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    			<p>Username &amp; Password:</p>
    			<input id="user" class="user" name="user" type="text">
    			<input id="pass" class="pass" name="pass" type="password">
    			<p>140 Character Message:</p>
    			<textarea id="twitter_stat" maxlength="140" name="twitter_stat" type="text"></textarea>
    			<input id="submit" name="submit" type="submit" value="Send" /><br />
    			<a href="http://<?php echo "$url";?>"><?php echo "$site";?></a>
    		</form>
    		<div class="clearer">
    		</div>
    	</div>
    </div>
    
    </body>
    
    </html>
    PHP:
     
    Last edited: Oct 23, 2009
    szalinski, Oct 23, 2009 IP
    jss4ever likes this.
  3. jss4ever

    jss4ever Well-Known Member

    Messages:
    354
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Oh my god.. i really dont know how to thank you ., im so amazed

    you really made my day by this

    thanks alot
     
    jss4ever, Oct 23, 2009 IP
  4. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    np, glad to see my work excites you :p
     
    szalinski, Oct 23, 2009 IP