views:

38

answers:

3

My server does not support cURL.

I want to update my status via php.

How to do that without cURL?

Again: WITHOUT CURL!

A: 

Here’s how you can tweet without using cURL with PHP. We have two options-

With stream context

Php function stream_context_create has the magic. It creates and returns a stream context with any options passed.

<?php
set_time_limit(0);
$username = 'username';
$password= 'WHATEVER';
$message='YOUR NEW STATUS';
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;
}
echo tweet($message, $username, $password);
?> 

With socket programing

PHP has a very capable socket programming API. These socket functions include almost everything you need for socket-based client-server communication over TCP/IP. fsockopen opens Internet or Unix domain socket connection.

<?php



$username = 'username';

$password= 'WHATEVER';

$message='YOUR NEW STATUS';



$out="POST http://twitter.com/statuses/update.json HTTP/1.1\r\n"

  ."Host: twitter.com\r\n"

  ."Authorization: Basic ".base64_encode ("$username:$password")."\r\n"

  ."Content-type: application/x-www-form-urlencoded\r\n"

  ."Content-length: ".strlen ("status=$message")."\r\n"

  ."Connection: Close\r\n\r\n"

  ."status=$msg";



$fp = fsockopen ('twitter.com', 80);

fwrite ($fp, $out);

fclose ($fp); 

?>

Taken from here: http://www.motyar.info/2010/02/update-twitter-status-with-php-nocurl.html

Hope htis helps.

If you need any more help let me know as i am a php programmer myself. thanks

PK

Pavan
A: 

You can do that using the oauth pecl extension. See here for details. EDIT: you need to install the pecl extension

RC
A: 

Pavan, this doesn't work with OAuth which is mandatory for Twitter.

RC, it tells me there's no oauth function. Does my server need to understand c?

Popular