开放的编程资料库

当前位置:我爱分享网 > PHP教程 > 正文

使用 cURL 远程发送 Twitter 直接消息

Twitter 的直接消息传递功能非常棒。虽然简单的推文传达的是“只是一个聪明的俏皮话”,但直接消息传达的是“听着这里 b*tch,这很重要,你需要现在通过短信接收这条消息。” Twitter 的 API 允许您远程直接发送消息,因此现在您可以让您的网站随时私信您(并称您为 b*tch)!

Shell cURL 命令

curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml

此示例显示了您在终端中键入的 cURL 命令。

PHP 使用

$result = shell_exec('curl -u davidwalshblog:myPass -d "text=Testing a remote direct message via C
URL&user=fellowTweeter" http://twitter.com/direct_messages/new.xml');

这就是您在 PHP 中使用该命令的方式。

示例 XML 响应

<!-- Successful! -->
<?xml version="1.0" encoding="UTF-8"?>
<direct_message>
  <id>186275699</id>
  <sender_id>15759583</sender_id>
  <text>Testing a direct message.</text>
  <recipient_id>15759583</recipient_id>
  <created_at>Sat Jun 20 00:13:41 +0000 2009</created_at>
  <sender_screen_name>davidwalshblog</sender_screen_name>
  <recipient_screen_name>davidwalshblog</recipient_screen_name>
  <sender>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>https://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </sender>
  <recipient>
    <id>15759583</id>
    <name>davidwalshblog</name>
    <screen_name>davidwalshblog</screen_name>
    <location>Madison, WI, US</location>
    <description>Flexile Programmer, MooTools Core Team Member, JavaScript Fanatic, CSS Tinkerer, PHP Hacker, and web lover.</description>
    <profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/57860553/footer-logo_normal.jpg</profile_image_url>
    <url>https://davidwalsh.name</url>
    <protected>false</protected>
    <followers_count>1834</followers_count>
    <profile_background_color>EEEEEE</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>2A447B</profile_link_color>
    <profile_sidebar_fill_color>CDCDCD</profile_sidebar_fill_color>
    <profile_sidebar_border_color>999999</profile_sidebar_border_color>
    <friends_count>20</friends_count>
    <created_at>Thu Aug 07 04:18:53 +0000 2008</created_at>
    <favourites_count>3</favourites_count>
    <utc_offset>-21600</utc_offset>
    <time_zone>Central Time (US & Canada)</time_zone>
    <profile_background_image_url>http://s3.amazonaws.com/twitter_production/profile_background_images/15318223/twitter-background.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <statuses_count>1734</statuses_count>
    <notifications>false</notifications>
    <verified>false</verified>
    <following>false</following>
  </recipient>
</direct_message>



<!-- ERROR RESPONSE -->
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <request>/direct_messages/new.xml</request>
  <error>You cannot send messages to users who are not following you.</error>
</hash>

如果您的 DM 成功,则会有大量信息——如果您的尝试失败,则会有一个简单的错误响应。

那么为什么要自动化 DM?我认为实现直接消息自动化的一个方便的时间是当网站上发生事件时您想接收文本消息。假设您是一名销售人员,并且您的网站上有一个“请求电话”表单。由于您一天中大部分时间都不在办公室(开车),因此您无法查看电子邮件。无需设置文本消息/SMS 系统,只需设置一个 Twitter 帐户并利用 Twitter 的短信功能即可。

您是否想要来自您网站的自动推文或直接消息?

未经允许不得转载:我爱分享网 » 使用 cURL 远程发送 Twitter 直接消息

感觉很棒!可以赞赏支持我哟~

赞(0) 打赏