PHP code example of simplon / twitter

1. Go to this page and download the library: Download simplon/twitter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simplon / twitter example snippets




// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// where should twitter redirect the user?
$callbackUrl = 'https://yourdomain.com/twitter';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
    $oauthRequestTokenVo = $twitter->requestOauthRequestToken($callbackUrl);

    echo "Twitter redirect url:\n";
    echo $twitter->getAuthenticationUrl($oauthRequestTokenVo->getOauthToken());
    echo "\n\n";
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}



// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// retrieved params
$oauthToken = 'AuNV9QXXXXXXelElAAABT0C1Sw4';
$oauthVerifier = 'UpVzmg0tiNXXXwuddcmIxgIb5PVuSn';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
	// retrieve access tokens and profile data from user
	$oauthAccessTokenVo = $twitter->requestOauthAccessToken($oauthToken, $oauthVerifier);
	
	var_dump($oauthAccessTokenVo);
   
   // var_dump result would look something like this:
    
    // class Simplon\Twitter\OauthAccessTokenVo#4 (5) {
	// protected $oauthToken =>
	// string(50) "3197060333-xxxx4chX0Sega3iMF0r55PP96BAGyXXXFTwjpgW"
	// protected $oauthTokenSecret =>
	// string(45) "FeIpfZ1qK4jTaKXXXXTaQAlfny0dFgBV4K15vbnFd3XX"
	// protected $userId =>
	// string(10) "1234567899"
	// protected $screenName =>
	// string(12) "foobar user"
	// protected $xAuthExpires =>
	// string(1) "0"
	// }
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}



// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// user's prior retrieved access tokens
$accessToken = '3197060333-xxxx4chX0Sega3iMF0r55PP96BAGyXXXFTwjpgW';
$accessSecret = 'FeIpfZ1qK4jTaKXXXXTaQAlfny0dFgBV4K15vbnFd3XX';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
	// bind user account to twitter session
	$twitter->setOauthTokens($accessToken, $accessSecret);
	
	// publish tweet
	$response = $twitter->post('statuses/update', ['status' => 'My first tweet!']);
	
	// should hold all tweet specific data in an array
	var_dump($response); // $response['id'] holds the tweet ID
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}



// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// user's prior retrieved access tokens
$accessToken = '3197060333-xxxx4chX0Sega3iMF0r55PP96BAGyXXXFTwjpgW';
$accessSecret = 'FeIpfZ1qK4jTaKXXXXTaQAlfny0dFgBV4K15vbnFd3XX';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
	// bind user account to twitter session
	$twitter->setOauthTokens($accessToken, $accessSecret);
	
	// load tweet data by ID
	$response = $twitter->get('statuses/show/633611781445959680');
	
	// should hold all tweet specific data in an array
	var_dump($response);
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}



// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// user's prior retrieved access tokens
$accessToken = '3197060333-xxxx4chX0Sega3iMF0r55PP96BAGyXXXFTwjpgW';
$accessSecret = 'FeIpfZ1qK4jTaKXXXXTaQAlfny0dFgBV4K15vbnFd3XX';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
	// bind user account to twitter session
	$twitter->setOauthTokens($accessToken, $accessSecret);
	
	// destroy tweet by ID
	$response = $twitter->get('statuses/destroy/633611781445959680');
	
	// should hold all tweet specific data in an array
	var_dump($response);
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}



// your app's consumer tokens
$apiKey = 'E3yaXX2LvOFXXXXikwMUyvlQD';
$apiSecret = 'WkhXXZPbyFdLiHqDCkSEqJodxm5XdIPLuFNzs1sXXdv09ZXXX7';

// user's prior retrieved access tokens
$accessToken = '3197060333-xxxx4chX0Sega3iMF0r55PP96BAGyXXXFTwjpgW';
$accessSecret = 'FeIpfZ1qK4jTaKXXXXTaQAlfny0dFgBV4K15vbnFd3XX';

// twitter session with your credentials
$twitter = new \Simplon\Twitter\Twitter($apiKey, $apiSecret);

try
{
	// bind user account to twitter session
	$twitter->setOauthTokens($accessToken, $accessSecret);
	
	// upload image
	$response = $twitter->upload('http://example-image.png'); 

	// should hold all media specific data in an array
	var_dump($response); // $response['media_id'] holds the media ID
	
	// publish tweet w/ media
	$response = $twitter->post('statuses/update', ['status' => 'Crazy summer vacation!', 'media_ids' => $response['media_id']);
	
	// should hold all tweet specific data in an array
	var_dump($response); // $response['id'] holds the tweet ID
}
catch (\Simplon\Twitter\TwitterException $e)
{
    var_dump($e->getMessage());
}