note 91775 added to function.stream-context-create
| From: | fabien dot potencier at sensio dot com | Date: | Thu, 25 Jun 2009 08:55:16 +0000 |
| Subject: | note 91775 added to function.stream-context-create | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157460@lists.php.net to get a copy of this message | ||
Nice example from http://fabien.potencier.org/article/20/tweeting-from-php
:
Twitter is everywhere nowadays. Odds are eventually you will want to tweet from PHP. No need to use
one of the numerous PHP Twitter libraries, as tweeting is as simple as using the PHP built-in
file_get_contents() function:
function tweet($message, $username, $password)
{
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => sprintf("Authorization: Basic %s\r\n",
base64_encode($username.':'.$password)).
"Content-type: application/x-www-form-urlencoded\r\n",
'content' => http_build_query(array('status' => $message)),
'timeout' => 5,
),
));
$ret = file_get_contents('http://twitter.com/statuses/update.xml',
false, $context);
return false !== $ret;
}
Pretty easy, no? Using the tweet() function is of course a piece of cake:
tweet('From PHP, yeah...', 'fabpot', 'Pa$$');
As an added bonus, the function returns true if everything went fine, or false otherwise.
----
Server IP: 212.124.37.9
Probable Submitter: 88.73.70.51
----
Manual Page -- http://www.php.net/manual/en/function.stream-context-create.php
Edit -- https://master.php.net/note/edit/91775
Del: integrated -- https://master.php.net/note/delete/91775/integrated
Del: useless -- https://master.php.net/note/delete/91775/useless
Del: bad code -- https://master.php.net/note/delete/91775/bad+code
Del: spam -- https://master.php.net/note/delete/91775/spam
Del: non-english -- https://master.php.net/note/delete/91775/non-english
Del: in docs -- https://master.php.net/note/delete/91775/in+docs
Del: other reasons-- https://master.php.net/note/delete/91775
Reject -- https://master.php.net/note/reject/91775
Search -- https://master.php.net/manage/user-notes.php