【问题标题】:Post status + image with TwitterAPIExchange on Twitter在 Twitter 上使用 TwitterAPIExchange 发布状态 + 图像
【发布时间】:2018-03-02 00:30:04
【问题描述】:

我有以下工作代码,该代码只是在我的 Twitter 页面上发布状态和图像。

       require_once('TwitterAPIExchange.php');

        $settings = array(
            'oauth_access_token' => "xxx",
            'oauth_access_token_secret' => "xxx",
            'consumer_key' => "xxx",
            'consumer_secret' => "xxx"
        );

        $url = "https://api.twitter.com/1.1/statuses/update_with_media.json";
        $requestMethod = "POST";

        $tweetmsg = $_POST['post_text'];
        $twimage = "Images/twitter-icon.png";

        $postfields = array(
            'status' => $tweetmsg,
            'media[]' => "@{$twimage}"
        );
        try {
            $twitter = new TwitterAPIExchange($settings);
            $twitter->buildOauth($url, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest();

            echo "Success, you just tweeted!";
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    }

图像现在放置在我的项目中的文件夹(图像)中。我希望用户能够从他自己的电脑中挑选一张图片,写一个描述然后发推文。我有以下简单的 HTML 表单用于发布:

<form method="post" action="index.php">
<textarea id="post_text" name="post_text" type="text"></textarea>
<input type="file" name="post_file" id="post_file" multiple accept="image/*" value="Choose a file"/>
<button type="submit" id="btn_submit">Submit</button>
</from>

那么你们有什么可以帮助我解决问题的提示或指南吗?我是否以正确的方式思考,或者我应该以另一种方式解决问题?谢谢!

【问题讨论】:

    标签: php twitter


    【解决方案1】:

    接受的答案使用已弃用的 API 端点 https://dev.twitter.com/rest/reference/post/statuses/update_with_media

    这是一个可行的解决方案:

    // send image to Twitter first
    $url = 'https://upload.twitter.com/1.1/media/upload.json';
    $requestMethod = 'POST';
    
    $image = 'full/path/to/image.jpg';
    
    $postfields = array(
      'media_data' => base64_encode(file_get_contents($image))
    );
    
    $response = $twitter->buildOauth($url, $requestMethod)
      ->setPostfields($postfields)
      ->performRequest();
    
    // get the media_id from the API return
    $media_id = json_decode($response)->media_id;
    
    // then send the Tweet along with the media ID
    $url = 'https://api.twitter.com/1.1/statuses/update.json';
    $requestMethod = 'POST';
    
    $postfields = array(
      'status' => 'My amazing tweet'
      'media_ids' => $media_id,
    );
    
    $response = $twitter->buildOauth($url, $requestMethod)
      ->setPostfields($postfields)
      ->performRequest();
    

    【讨论】:

    • 这应该是已接受答案下的评论,或者如果您有另一个更新的答案要提供,您应该编辑并包含上下文。
    • 我想我会链接到另一篇文章以避免重复,但你去吧。
    • 如果答案适用,您应该将该问题标记为重复。 (不是反对者,顺便说一句)
    • 我已经删除了我在另一篇文章中的答案,并将其标记为重复,因为这是旧的。
    • 这应该集成到 TwitterAPIExchange 中
    【解决方案2】:

    你必须在这里实现轻微的逻辑—— 现在点击提交做以下 1.将图像存储在项目的某个文件夹中(参考下面的代码将图像存储在文件夹中)

    上传文件.php //这里上传文件代码

    2.存储文本并在数据库中上传图像名称。 3.现在图像文件夹中的图像...并且您的数据库中也有图像名称和相应的消息。

    4。 $tweetmsg = $_POST['post_text']; $twimage = "图片/twitter-icon.png"; //在此之前

       retrive the last inserted row and fetch message and image name 
    
       $tweetmsg = $row['msg'];
       $image = $row['image_name'];
        $twimage = "Images/".$image;
    

    我希望这对你有用..谢谢

    【讨论】:

    【解决方案3】:

    这比我想象的要容易得多,我只是使用 $_FILES 将图像存储在一个变量中,这样我就可以在 $postfields 数组中使用它。

    $url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json";
    $requestMethod = "POST";
    
    $tweetmsg = $_POST['post_description'];
    $twimg = $_FILES['pictureFile']['tmp_name'];
    
        $postfields = array(
            'status' => $tweetmsg,
            'media[]' => '@' . $twimg
        );
        try {
            $twitter = new TwitterAPIExchange($settings);
            $twitter->buildOauth($url_media, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest();
    
            echo "You just tweeted with an image";
        } catch (Exception $ex) {
            echo $ex->getMessage();
        }
    

    【讨论】:

      猜你喜欢
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 2014-04-09
      • 1970-01-01
      相关资源
      最近更新 更多