tag:blogger.com,1999:blog-55425005970752533872024-03-19T03:10:08.329-07:00Source ExplorerIron Manhttp://www.blogger.com/profile/02903543509940447721noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-5542500597075253387.post-91518716550507958412014-03-22T12:33:00.000-07:002014-03-22T13:17:13.631-07:00How to post tweet to Twitter using Php <div dir="ltr" style="text-align: left;" trbidi="on">
Here's my first tutorial for beginners which shows how to post or send tweet to Twitter using php code. It's very simple just follow this tutorial.<br />
<br />
<h3 style="text-align: left;">
<b>What you need?</b></h3>
<div>
<ul style="text-align: left;">
<li>A system installed php. If you don't have, get <a href="http://www.apachefriends.org/download.html" target="_blank">Xampp from here</a>. Install once your download gets finished. </li>
<li>Next, is Abraham's Php Library. You can get it right from <a href="https://github.com/abraham/twitteroauth" target="_blank">here</a>. Click download zip, extract it & save it in a separate folder.</li>
<li>Sublime Editor. (optional but it's good when you do coding).</li>
</ul>
<div>
<span style="color: red;"><br /></span></div>
<div>
<span style="color: red;">Note</span> : Click Pictures to view in big size.</div>
<div>
<br /></div>
<h3 style="text-align: left;">
Steps to send tweet to Twitter : </h3>
</div>
<div>
<br /></div>
1. Go to <a href="https://apps.twitter.com/" target="_blank">Twitter Developer page </a>first to create an app. You need to first sign-in with your current twitter username and password to proceed.<br />
<br />
2. Click Create new app.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii44StQUGxFKMlP0anHfMLuTpGmFRP9yUbsTR5E5PF_R_jydhtW68rO7-nZzWxTq-byNck5cXFfuTsjc9-pUwbYdFKBIHHYkEBhp41Y3v0QzvSTPGM2v_8H8P-U85HUoAm6bV4q21yqew/s1600/Screenshot+from+2014-03-22+23:50:26.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEii44StQUGxFKMlP0anHfMLuTpGmFRP9yUbsTR5E5PF_R_jydhtW68rO7-nZzWxTq-byNck5cXFfuTsjc9-pUwbYdFKBIHHYkEBhp41Y3v0QzvSTPGM2v_8H8P-U85HUoAm6bV4q21yqew/s1600/Screenshot+from+2014-03-22+23:50:26.png" height="65" width="400" /></a></div>
<br />
3. In Create an application page, fill Application name, App Description and Website URL. If you don't have a website, just paste your Twitter profile link.<br />
<br />
<br />
<div class="d-block d-block-system g-main" style="background-color: white; color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 21px; text-align: -webkit-auto;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3r0me2yuF8cEpqXOkJMdxEnbvRVOcVrSZz241wf9TenVLwqioK1X47tU9CC8tNhn1MPQOrqSEsUDKdPAStli-4Lb9kBfmHpQNdPLcWw-oKfJ-TfZRNa-K50yFj4zavFkc4_03kBdgr3o/s1600/Screenshot+from+2014-03-22+23:57:40.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh3r0me2yuF8cEpqXOkJMdxEnbvRVOcVrSZz241wf9TenVLwqioK1X47tU9CC8tNhn1MPQOrqSEsUDKdPAStli-4Lb9kBfmHpQNdPLcWw-oKfJ-TfZRNa-K50yFj4zavFkc4_03kBdgr3o/s1600/Screenshot+from+2014-03-22+23:57:40.png" height="236" width="400" /></a></div>
<br />
<div style="text-align: -webkit-auto;">
Once you filled tick <span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><b>Yes i agree</b> & click <b>Create application.</b></span></span></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><b><br /></b></span></span></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><br /></span></span></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;">4. You can see the following screenshot once your application created without any problem. </span></span></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><br /></span></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS-XYNlmtVLCcBLG65OE_Qord-rN5KTm90BGEk09bAdUMRPfZlK5ZVpX6ay2Ypwn7GTAXD_ODc2_350sE6CHHIRyXlTim1CsjfMks9yuqrpB0UMxQeJgZvzhiic84GeNleX6GsMQg4Y7k/s1600/Screenshot+from+2014-03-23+00:01:40.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS-XYNlmtVLCcBLG65OE_Qord-rN5KTm90BGEk09bAdUMRPfZlK5ZVpX6ay2Ypwn7GTAXD_ODc2_350sE6CHHIRyXlTim1CsjfMks9yuqrpB0UMxQeJgZvzhiic84GeNleX6GsMQg4Y7k/s1600/Screenshot+from+2014-03-23+00:01:40.png" height="213" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><br /></span></span></div>
<div style="text-align: -webkit-auto;">
<blockquote class="tr_bq">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">The next thing you need to do is to change the app access permission to <b>write</b> since we gonna post tweet to twitter. For to do, Goto - </span><b style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">Permissions</b><span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> Tab - Change the access type to </span><b style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">read and write - </b><span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">Click</span><b style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> Update setting.</b></blockquote>
</div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><br /></span></span></div>
<div style="text-align: -webkit-auto;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ0K2QzPEVpU2AIsIuafdIsT10GjejT08CObQkxoO-gSq3UDtyg_g64TicIgLX4qHH8_Tp7_Eq9dxASSeAhfb3Jt1YLdiR0NI4UWLfEYmFHouynVXdccdkzWR-_WTxQO7vpxkg6ndIhcE/s1600/Screenshot+from+2014-03-23+00:03:20.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQ0K2QzPEVpU2AIsIuafdIsT10GjejT08CObQkxoO-gSq3UDtyg_g64TicIgLX4qHH8_Tp7_Eq9dxASSeAhfb3Jt1YLdiR0NI4UWLfEYmFHouynVXdccdkzWR-_WTxQO7vpxkg6ndIhcE/s1600/Screenshot+from+2014-03-23+00:03:20.png" height="168" width="400" /></a></div>
<div style="text-align: -webkit-auto;">
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"><br /></span></span>
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;">5. Visit <b>API Keys</b> Tab, scroll down and click <b>Create my access token</b>.</span></span><br />
<br />
<br />
<span style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;"><span style="font-size: 14px; line-height: 20px;"> </span></span><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-k4GQE07KLPXFroHuWXw3kAM_8zAN2EFR6ImgPlyGbOf3rnPjAzD7HAA82q8QpfxAX36zFur2CWVWZVRovvS5X4secRpnRK9ICEBqhJ7sbvwaoO4812DUuNZ7KmMWrIuEzsXOZyXB9TY/s1600/Screenshot+from+2014-03-23+00:09:43.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-k4GQE07KLPXFroHuWXw3kAM_8zAN2EFR6ImgPlyGbOf3rnPjAzD7HAA82q8QpfxAX36zFur2CWVWZVRovvS5X4secRpnRK9ICEBqhJ7sbvwaoO4812DUuNZ7KmMWrIuEzsXOZyXB9TY/s1600/Screenshot+from+2014-03-23+00:09:43.png" height="120" width="400" /></a></div>
<br />
6. Note down <b>API Key, API Secret, Access Token & Access Token Secret </b>and keep it in safe<b>.</b><br />
<br />
<br />
7. Go to Xampp folder which is in C Drive. And in that, open <b>htdocs</b> folder. Delete Index.php & Index.html if you find anything in htdocs folder. (This is for beginners)<br />
<br />
<br />
8. Create a new folder i call it as <b><span style="color: red;">FirstApp2014</span></b>. Copy only <b>twitteroauth</b> folder from Abraham Twitter Php Library (i.e the one you downloaded from <a href="https://github.com/abraham/twitteroauth" target="_blank">here</a>) and paste it in FirstApp2014 folder.<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTV60VfZ1a7KvviB2LwkjlDuAm9uGx2Ce3GvkiaL-ZdFbb59IiMqWtwlSigGSd2IyCRTAhGK03TlV0jQxNZ25N_CJ4tHChe2G0qP7_FF5eFpxJJWCuAf1CO20-uLcTJaG0Ro-NbNTZyfY/s1600/Screenshot+from+2014-03-23+00:22:01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTV60VfZ1a7KvviB2LwkjlDuAm9uGx2Ce3GvkiaL-ZdFbb59IiMqWtwlSigGSd2IyCRTAhGK03TlV0jQxNZ25N_CJ4tHChe2G0qP7_FF5eFpxJJWCuAf1CO20-uLcTJaG0Ro-NbNTZyfY/s1600/Screenshot+from+2014-03-23+00:22:01.png" height="88" width="400" /></a></div>
<br />
<br />
9. Your FirstApp2014 folder now should only have twitteroauth folder. Let create <b>test.php </b>using any editor ( i prefer sublime) - save it in <b>FirstApp2014 </b>folder.<br />
<br />
10. First add this code to include Abraham Twitter library,<br />
<span style="color: red;"><br /></span>
<br />
<div style="text-align: left;">
<span style="color: red;"><?php</span></div>
<div style="text-align: left;">
<span style="color: red;">require_once('twitteroauth/twitteroauth.php'); </span></div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
Next is,</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="color: red;">$CONSUMER_KEY = " paste consumer key here which you got from Twitter apps page";</span></div>
<div style="text-align: left;">
<span style="color: red;">$CONSUMER_SECRET = "paste consumer secret here";</span></div>
<div style="text-align: left;">
<span style="color: red;">$ACCESS_TOKEN = "paste access token here";</span></div>
<div style="text-align: left;">
<span style="color: red;">$ACCESS_SECRET = "paste access secret here";</span></div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
To make a connection to twitter add the following lines,</div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
<span style="color: red;">$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN , $ACCESS_SECRET);</span></div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
The following line verify the credentials,</div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
<span style="color: red;">$content = $connection->get('account/verify_credentials');</span></div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
The code to post tweet is,</div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
<span style="color: red;">$connection->post('statuses/update', array('status' => "Hai this is my first tweet"));</span></div>
<div style="text-align: left;">
<span style="color: red;"><br /></span></div>
<div style="text-align: left;">
Replace your text above what you want to tweet through app.</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
Here's the full code for <b>test.php :</b></div>
<div style="text-align: left;">
<b><br /></b></div>
<div style="text-align: left;">
</div>
<span style="color: red;"><?php</span><br />
<span style="color: red;">require_once('twitteroauth/twitteroauth.php'); </span><br />
<br />
<div style="text-align: left;">
</div>
<span style="color: red;">$CONSUMER_KEY = " paste consumer key which you got from Twitter apps page";</span><br />
<span style="color: red;">$CONSUMER_SECRET = "paste consumer secret here";</span><br />
<span style="color: red;">$ACCESS_TOKEN = "paste access token here";</span><br />
<span style="color: red;">$ACCESS_SECRET = "paste access secret here";</span><br />
<span style="color: red;">$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_TOKEN , $ACCESS_SECRET);</span><br />
<br />
<span style="color: red;">$content = $connection->get('account/verify_credentials');</span><br />
<br />
<span style="color: red;">$connection->post('statuses/update', array('status' => "Hai this is my first tweet"));</span><br />
<div>
<span style="color: red;">echo "Tweet sent";</span></div>
<div>
<span style="color: red;">?> </span></div>
<div>
<span style="color: red;"><br /></span></div>
<div>
11. Once you're done open any browser and type <span style="color: blue;">localhost/FirstApp2014/test.php </span>to execute this script in order to tweet. You can see your tweet in your user timeline. Enjoy! :)</div>
<br />
<div>
<span style="color: red;"><br /></span></div>
<br />
<span style="color: red;"><br /></span>
</div>
Iron Manhttp://www.blogger.com/profile/02903543509940447721noreply@blogger.com0