Php 通过API发布到Twitter

Php 通过API发布到Twitter,php,api,twitter,Php,Api,Twitter,我正试图找出如何让我的用户通过我的网站上的链接发布推文。这是我设计的过程: 我网站上的某个链接会提供一个优惠:“发送Twit并收到优惠券” 一旦他们点击了offer链接,他们就会被重定向到Twitter进行授权 在他们授予与他们的Twitter个人资料连接的权限后,我想使用Twitter API发送一篇帖子,其中包含一条预先填充的消息,比如:“免费试用此在线工具:http”//mylink.com”。在发布消息后,他们需要重定向回我的站点,并返回到带有优惠券代码的页面 到目前为止,我大约完成了7

我正试图找出如何让我的用户通过我的网站上的链接发布推文。这是我设计的过程:

  • 我网站上的某个链接会提供一个优惠:“发送Twit并收到优惠券”
  • 一旦他们点击了offer链接,他们就会被重定向到Twitter进行授权
  • 在他们授予与他们的Twitter个人资料连接的权限后,我想使用Twitter API发送一篇帖子,其中包含一条预先填充的消息,比如:“免费试用此在线工具:http”//mylink.com”。在发布消息后,他们需要重定向回我的站点,并返回到带有优惠券代码的页面
  • 到目前为止,我大约完成了75%

  • 我安装了
  • 根据演示示例,我现在可以执行以下操作: --链接到Twitter() --使用Twitter登录用户 --重定向回我的站点
  • 我仍然需要弄清楚的是如何发布Tweet…理想情况下,我希望显示将发布的消息,但没有编辑它的功能,只是为了让他们知道。我可能会在第一页显示它,这意味着一旦他们授予Twitter权限,我想在重定向到co之前自动发布它根据代码。我在哪里添加POST功能

    下面是return.php代码

    <?php
    /* Start session and load library. */
    session_start();
    require_once('twitteroauth/twitteroauth.php');
    require_once('config.php');
    
    // This is where we end up when the user comes back from twitter.
    // First, we creat a new connection object
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    // Then we use it to send a twitter message
    
    $connection->post('statuses/update', array('status' => 'Test message'));
    
    // Finally, we redirect the user to the coupon page
    header('Location: /privacy'); // Supplies user with coupon
    ?>
    

    当Twitter将用户重定向回您的站点时,发送推文,然后加载带有优惠券代码的页面。尽管如此,请确保您已通知用户,如果用户授权您的应用,他们的状态将更新

    您忘记交换访问令牌:

    <?php
    /* Start session and load library. */
    session_start();
    require_once('twitteroauth/twitteroauth.php');
    require_once('config.php');
    
    // This is where we end up when the user comes back from twitter.
    // First, we creat a new connection object
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    
    $token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);
    $_SESSION['oauth_token'] = $token_credentials['oauth_token']
    $_SESSION['oauth_token_secret'] = $token_credentials['oauth_token_secret'];
    
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    
    // Then we use it to send a twitter message
    
    $connection->post('statuses/update', array('status' => 'Test message'));
    
    // Finally, we redirect the user to the coupon page
    header('Location: /privacy'); // Supplies user with coupon
    ?>
    
    
    
    是的,我会在他们进入Twitter甚至显示更新文本之前先说明清楚。我正在努力的是如何发布消息。请用代码查看我的更新问题。@santa检查我在第11-13行中添加的代码,以获取访问令牌。我使用的是相同的代码。使用此代码,用户可以在I上发布r在登录后打开twitter页面,但我想在没有登录功能的情况下在我的页面上发布。我怎么做?