Twitter Oauth通过PHP没有cURL

Pop*_*lar 3 php twitter curl

我的服务器不支持cURL.

我想通过php更新我的状态.

没有cURL怎么做?

再次:没有卷曲!

Pav*_*van 6

以下是如何在不使用cURL和PHP的情况下发送推文.我们有两个选择 -

使用流上下文

Php函数stream_context_create具有魔力.它使用传递的任何选项创建并返回流上下文.

<?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);
?> 
Run Code Online (Sandbox Code Playgroud)

使用套接字编程

PHP有一个非常强大的套接字编程API.这些套接字函数几乎包含了基于套接字的客户端 - 服务器通过TCP/IP进行通信所需的一切.fsockopen打开Internet或Unix域套接字连接.

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

?>
Run Code Online (Sandbox Code Playgroud)

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

希望htis有所帮助.

如果您需要更多帮助,请告诉我,因为我自己是一名php程序员.谢谢

PK