Twitter Oauth via PHP WITHOUT cURL

My server does not support cURL.

I want to update my status using php.

How to do this without cURL?

Again: WITHOUT CURL!

+3
source share
3 answers

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

With thread context

The php function stream_context_create has magic. It creates and returns a stream context with any parameters 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 programming

PHP has a very convenient socket programming API. These socket functions include almost everything you need to communicate between socket-based servers over TCP / IP. fsockopen opens a socket connection in an Internet or Unix domain.

<?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); 

?>

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

, .

, , php.

+5
    <?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;
}
?>
+2

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

0
source

Source: https://habr.com/ru/post/1767739/


All Articles