{"id":228,"date":"2010-12-17T15:13:31","date_gmt":"2010-12-17T15:13:31","guid":{"rendered":"http:\/\/www.osd.net\/blog\/?p=228"},"modified":"2014-09-09T08:33:34","modified_gmt":"2014-09-09T08:33:34","slug":"update-twitter-status-using-php","status":"publish","type":"post","link":"https:\/\/www.osd.net\/blog\/web-development\/php\/update-twitter-status-using-php\/","title":{"rendered":"Update twitter status using php"},"content":{"rendered":"<p>After my first <a title=\"Post to facebook wall using php\" href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/post-to-facebook-wall-using-php\/\">post about posting on facebook wall<\/a> had some feedback I decided to create another tutorial for <strong>twitter <\/strong>since the system is very similar.<\/p>\n<p>Twitter also uses oauth, so you will need to ask for the user&#8217;s permission in order to update his status (once again the &#8220;old&#8221; methods that allowed you to do anything you like if you find out the password are useless and this is a good thing).<\/p>\n<p>Another good reason to write this post was the fact that most of the examples (and the highest ranked on google) you find now on the web use the <strong>old method<\/strong> to update the status. That system was very simple but it is <strong>not working<\/strong> any more (since twitter switched to oauth only). So if you have a script that wants to do this just by calling an url using <strong>curl <\/strong>and authenticating with username and password stop debugging it as it <strong>will never work<\/strong>.<\/p>\n<h1>Create twitter application<\/h1>\n<p>As I said, the system is very similar to the one used on facebook so if you are familiar to it you will find this familiar. If not after you will understand this I am sure that you will be able to understand the <a title=\"Post to facebook wall using php\" href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/post-to-facebook-wall-using-php\/\">other post<\/a> also.<\/p>\n<p>So the first thing is to create a <strong>twitter application<\/strong>. For this, after you log in, go to <a title=\"Create new twitter application\" href=\"http:\/\/dev.twitter.com\/apps\/new\">http:\/\/dev.twitter.com\/apps\/new<\/a> an fill in that form. Make sure that you enter a valid <strong>callback url<\/strong>, that points to the script that will save the authentication token. (so let&#8217;s say that it will be something like http:\/\/www.example.com\/tw_users.php).<\/p>\n<p>Also remember to check &#8220;<strong>Read and Write<\/strong>&#8221; under <strong>Default Access Type<\/strong>.<\/p>\n<div id=\"attachment_229\" style=\"width: 698px\" class=\"wp-caption aligncenter\"><a title=\"View full\" href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app.jpg\" target=\"_blank\" rel=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app.jpg\"><img aria-describedby=\"caption-attachment-229\" loading=\"lazy\" class=\"size-full wp-image-229  \" title=\"Create twitter application\" src=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app.jpg\" alt=\"Create twitter application\" width=\"688\" height=\"431\" srcset=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app.jpg 984w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app-300x188.jpg 300w, https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/tw_app-150x94.jpg 150w\" sizes=\"(max-width: 688px) 100vw, 688px\" \/><\/a><p id=\"caption-attachment-229\" class=\"wp-caption-text\">Create twitter application<\/p><\/div>\n<p>After you filled all the fields submit this form. You will be provided with an <strong>api key<\/strong> (customer key) and a <strong>customer secret<\/strong>. You will need these values in your scripts.<\/p>\n<h1>Get user approval<\/h1>\n<p>Now we will create the script that <strong>asks <\/strong>the user for <strong>permissions <\/strong>to update his status. For this (and the following operations) I used a library I found google-ing for resources. You will find that library in the archive you can download after you complete reading this post.<\/p>\n<p>This script will do a simple thing: redirect the user to twitter with some parameters set so that he will be asked for approval.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);\r\n\t$request_token = $connection-&gt;getRequestToken(OAUTH_CALLBACK . '?user=' . $_GET['user']);\r\n\t$token = $request_token['oauth_token'];\r\n\t$url = $connection-&gt;getAuthorizeURL($token);\r\n<\/pre>\n<h1>Saving his access token<\/h1>\n<p>After the user will allow your application to update his status he will be redirected to that callback url you provided when you created the application. To that url we added the <strong>username <\/strong>(so that this application will be able to post updates at anytime for a user that gave you his username and accepted to use it)<\/p>\n<p>So when the user gets to this page we will save the access token like this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);\r\n$access_token = $connection-&gt;getAccessToken($_REQUEST['oauth_verifier']);\r\n$userData['Twitter_Access_Token'] = $access_token['oauth_token'];\r\n$userData['Twitter_Access_Token_Secret'] = $access_token['oauth_token_secret'];\r\n$user_tw_username = @$_GET['user'];\r\n\/\/ save these fields in our database\r\n<\/pre>\n<p>The hard part is over now you just need to use the info you have.<\/p>\n<h1>Posting new status<\/h1>\n<p>Now that you have the <strong>access token<\/strong> you can post updates for this user at any time.<\/p>\n<p>The only thing that can change this (and that you might need while testing) is the user revoking your access.<\/p>\n<p>For this go to your twitter account -&gt; settings -&gt; connections and click to the &#8220;<strong>revoke access<\/strong>&#8221; link next to the name of the application you just created.<\/p>\n<p>Now the easiest but most important part: sending the <strong>new status<\/strong>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\t$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $info['tw_user_access_token'], $info['tw_access_token_secret']);\r\n\r\n\t$result = $connection-&gt;post(\r\n\t\t'statuses\/update',\r\n\t\tarray(\r\n\t\t\t'status' =&gt; $message\r\n\t\t)\r\n\t);\r\n<\/pre>\n<p>That&#8217;s all, you just posted your first update on twitter using <strong>php<\/strong> \ud83d\ude42<\/p>\n<p>For the full working example click <a title=\"Full working example\" href=\"https:\/\/www.osd.net\/blog\/wp-content\/uploads\/2010\/12\/twapp.zip\">here<\/a>.<\/p>\n<p>In the archive you will find all the sources you need (including that library I found for which I don&#8217;t want to take credit.. you will find author contact details in the files) and one file named README.txt that tell you what you need to customize for this script to work.<\/p>\n<p>I hope the post was clear enough and maybe it will help someone. If you have any questions or feedback please use the comment form below.<\/p>\n<div id=\"share-and-beer-container\">\t<div id=\"buy_me_a_beer_div\" class=\"single beer\">\n\t \t\n\t\t<div class=\"buy-beer\" onclick=\"document.getElementById('buy_me_a_beer_form').submit();\">\n\t\t\t<form action=\"https:\/\/www.paypal.com\/cgi-bin\/webscr\" id=\"buy_me_a_beer_form\" method=\"post\">\n\t\t\t\t<input type=\"hidden\" name=\"cmd\" value=\"_xclick\" \/>\n\t\t\t\t<input type=\"hidden\" name=\"business\" value=\"info@directaccess.ro\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_name\" value=\"A Beer For Update twitter status using php\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"item_number\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"return\" value=\"https:\/\/www.osd.net\/blog\/web-development\/php\/update-twitter-status-using-php\/\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"amount\" value=\"5\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"undefined_quantity\" value=\"1\" \/>  \n\t\t\t\t<input type=\"hidden\" name=\"currency_code\" value=\"USD\" \/>  \n\t\t\t<\/form>\n\t\t\t<p class=\"buy-beer-text\">If you liked this post <br \/> you can <strong>buy me a beer<\/strong><\/p>\n\t\t<\/div>\n\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>After my first post about posting on facebook wall had some feedback I decided to create another tutorial for twitter since the system is very similar. Twitter also uses oauth, so you will need to ask for the user&#8217;s permission in order to update his status (once again the &#8220;old&#8221; methods that allowed you to &hellip;<\/p>\n<div class=\"cta1\"><a href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/update-twitter-status-using-php\/\">Read more<\/a><\/div>\n<div class=\"like-excerpt\"><div\n        class=\"fb-like\"\n        data-href=\"https:\/\/www.osd.net\/blog\/web-development\/php\/update-twitter-status-using-php\/\"\n        data-layout=\"button_count\"\n        data-action=\"like\"\n        data-show-faces=\"false\"\n        data-share=\"false\">\n        <\/div><\/div>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[35,58,59,60],"_links":{"self":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/228"}],"collection":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":14,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":1291,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions\/1291"}],"wp:attachment":[{"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.osd.net\/blog\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}