php - Twitter Oauth 通过 PHP WITHOUT cURL

标签 php twitter curl

我的服务器不支持 cURL。

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

没有 cURL 怎么办?

再一次:没有 curl !

最佳答案

下面是如何在不使用 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);
?> 

用套接字编程

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

?>

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

希望对您有所帮助。

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

PK

关于php - Twitter Oauth 通过 PHP WITHOUT cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3849415/

相关文章:

PHP 邮件() 函数

php - 如何阻止变量在表单提交后重置?

php - Twitter oauth2/invalidate_token 错误 "Unable to verify your credentials"、 "authenticity_token_error"

curl - cURL: block 编码数据中的[P] roblem(2)

api - 如何在 unix 中通过 curl 进行 Twitter API 调用

php - mysql 表中的字段向上排序

java - 使用 PHP 从 Java 生成器提供内存中的 pdf

api - 创建 API 会给您的服务带来什么好处?

java - Twitter api 身份验证的 Try-catch 问题

curl - 使用 Awk 或 Sed 与 Curl 进行优化