It does not connect with the twitter app

2

I have this error that comes up in the browser when I try to connect to a twitter app:

  

Fatal error: Can not use method return value in write context in   /home/abc/public_html/publisher2/application/controllers/twitterapp.php   on line 41

on line 41 is the following:

$oauth_token = empty($this->session->userdata('oauth_request_token')) ? $_SESSION['oauth_request_token'] : $this->session->userdata('oauth_request_token');

What do you consider the error to be?

The content of the complete file is this:

<?php session_start();

class TwitterApp extends Controller {

    private $_consumer_key = CONSUMER_KEY;
    private $_consumer_secret = CONSUMER_SECRET;    

    function TwitterApp()
    {
        parent::Controller();
        $this->load->library('twitter');
    }

    public function index()
    {
        $tokens['access_token'] = NULL;
        $tokens['access_token_secret'] = NULL;

        // GET THE ACCESS TOKENS        
        $to = $this->twitter->TwitterOAuth($this->_consumer_key, $this->_consumer_secret, $oauth_token = false, $oauth_token_secret = false);

        //Request tokens from twitter 
        $OAUTH_CALLBACK = site_url('twitterapp/callback/ch/');
        $tok = $this->twitter->getRequestToken($OAUTH_CALLBACK);


        // Set session
        $_SESSION['oauth_request_token'] = $token = $tok['oauth_token'];
        $_SESSION['oauth_request_token_secret'] = $tok['oauth_token_secret'];

        /* Build the authorization URL*/
        $request_link = $this->twitter->getAuthorizeURL($tok['oauth_token']);

        header("Location: $request_link");
    }

    public function callback()
    {

        $userid = $this->session->userdata('user_id');
        $oauth_token = empty($this->session->userdata('oauth_request_token')) ? $_SESSION['oauth_request_token'] : $this->session->userdata('oauth_request_token');
        $oauth_token_secret = empty($this->session->userdata('oauth_request_token_secret')) ? $_SESSION['oauth_request_token_secret'] : $this->session->userdata('oauth_request_token_secret');
        $this->twitter->TwitterOAuth($this->_consumer_key, $this->_consumer_secret, $oauth_token, $oauth_token_secret);

        $url = parse_url($_SERVER['REQUEST_URI']);
        parse_str($url['query'], $params);

        // get access token and secret
        $info = $this->twitter->getAccessToken($params['oauth_verifier']);

        // Get account details
        $userInfo = $this->twitter->get("users/show", array("user_id" => $info['user_id']));
        //d($userInfo,1);

        if($params['denied'])
        {
            $this->session->set_userdata(array("type" => "error", "message" => "You said no thanks..."));
        }elseif(!$userid){
            $this->session->set_userdata(array("type" => "error", "message" => "Please login before adding facebook profile."));
            redirect('login'); exit;
        }
        else if($userInfo->errors[0]->message)
        {
            $this->session->set_userdata(array("type" => "error", "message" => "Sorry! You have following errors: " .$userInfo->errors[0]->message ));
        }
        else
        {
                $profile_id = $info['user_id'];
                $query = $this->db->get_where( "profile", array( 'profile_type' => 'twitter', 'profile_id' => $profile_id ) );
                $profile = $query->row_array();

                if($query->num_rows() == 0 ) {


                    // Save account info.
                    $this->db->insert("profile", array( "profile_id" => $info['user_id'], "profile" => $info['screen_name'], "display_name" => $userInfo->name, "autho_token" => $info['oauth_token'], "autho_token_secret" => $info['oauth_token_secret'], "profile_picture" => $userInfo->profile_image_url, "profile_type" => 'twitter', "user_id" => $userid ));


                }else{

                    $updata["profile"] = $info['screen_name'];
                    $updata["display_name"] = $userInfo->name;
                    $updata["autho_token"] = $info['oauth_token'];
                    $updata["autho_token_secret"] = $info['oauth_token_secret'];
                    $updata["profile_picture"] = $userInfo->profile_image_url;
                    $this->db->update("profile", $updata, array( 'profile_type' => 'twitter', "profile_id" => $profile_id ));   

                    if($profile['user_id'] != $userid)
                        $this->session->set_userdata(array("type" => "error", "message" => "Sorry! This account is already taken by another person."));             

                }

        }
        redirect('connector');
    }

}
?>
    
asked by Ivan Diaz Perez 28.06.2016 в 00:32
source

1 answer

1

I to tweet from php use these functions, with the compression of the link with bit.ly

function tinyurl($url_larga){
    $tiny = "http://api.bit.ly/v3/shorten?login=o_30ods7tgcl&apiKey=R_9d2e154e3e593244e0ecf67e54ae5b18&format=txt&longUrl=".$url_larga;
    $sesion = curl_init();
    curl_setopt ( $sesion, CURLOPT_URL, $tiny );
    curl_setopt ( $sesion, CURLOPT_RETURNTRANSFER, 1 );
    $url_tiny = curl_exec ( $sesion );
    curl_close( $sesion );
    return($url_tiny);
}

//declaramos la función que realiza la conexión a tu aplicación de twitter
function getConnectionWithAccessToken() {
    $connection = new TwitterOAuth(_CONSUMER_KEY, _CONSUMER_SECRET,_OAUTH_TOKEN, _OAUTH_TOKEN_SECRET);
    return $connection;
}   

function twittear($mensaje,$link)
{

    $hastag='' //Hastag de tu twitter;

    if ($link!=''){ //Por si quieres los enlaces acortarlos con bit.ly, necesita registro
        $bit=tinyurl($link); //reducimos el link con la api de bit.ly
        $quedan=(140-strlen($bit))-4; // calculo los caracteres restantes que me quedan para publicar restando los puntos suspensivo
        $mensaje=substr(utf8_encode($mensaje),0,$quedan). ' ' . $bit . ' ' . $hastag; // corto el mensaje en caso de que sea muy largo
    } else{
        $mensaje=$mensaje . ' ' . $hastag; // corto el mensaje en caso de que sea muy largo
        //$mensaje=$mensaje . ' ' . $hastag;
    }

    ini_set('display_errors', 1);
    require_once('TwitterAPIExchange.php');
    /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
    $settings = array(
        'oauth_access_token' => "",
        'oauth_access_token_secret' => "",
        'consumer_key' => "",
        'consumer_secret' => ""
    );

    $url = 'https://api.twitter.com/1.1/statuses/update.json';
    $requestMethod = 'POST';
    /** POST fields required by the URL above. See relevant docs as above **/
    $postfields = array( 'status' => $mensaje, );

    /** Perform a POST request and echo the response **/
    $twitter = new TwitterAPIExchange($settings);
    return $twitter->buildOauth($url, $requestMethod)->setPostfields($postfields)->performRequest();

}
    
answered by 28.06.2016 / 20:43
source