3

My server does not support cURL.

I want to update my status via php.

How to do that without cURL?

Again: WITHOUT CURL!

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

Comments

2
    <?php
/*
* using file_get_contents
*/

$key = '';
$secret = '';
$api_endpoint = 'https://api.twitter.com/1.1/search/tweets.json?q=news'; // endpoint must support "Application-only authentication"

// request token
$basic_credentials = base64_encode($key.':'.$secret);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  =>    'Authorization: Basic '.$basic_credentials."\r\n".
        "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n",
        'content' => 'grant_type=client_credentials'
    )
);

$context  = stream_context_create($opts);

// send request
$pre_token = file_get_contents('https://api.twitter.com/oauth2/token', false, $context);

$token = json_decode($pre_token, true);

if (isset($token["token_type"]) && $token["token_type"] == "bearer"){
    $opts = array('http' =>
        array(
            'method'  => 'GET',
            'header'  => 'Authorization: Bearer '.$token["access_token"]       
        )
    );

    $context  = stream_context_create($opts);

    $data = file_get_contents($api_endpoint, false, $context);

    print $data;
}
?>

1 Comment

How would you wire your code sample to post a tweet? I changed the endpoint to api.twitter.com/1.1/statuses/update.json?status=Testing and it causes me to get a 400 Bad Request. From what the docs say, this probably means I am not authenticated to post a tweet. I believe you need more auth credentials than just the key and secret?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.