Twitter上的TwitterAPIExchange发布状态+图片

问题描述 投票:0回答:3

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

       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
3个回答
3
投票

接受的答案使用了弃用的API端点qazxsw poi

这是一个有效的解决方案:

https://dev.twitter.com/rest/reference/post/statuses/update_with_media

0
投票

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

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

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

4. $ tweetmsg = $ _POST ['post_text']; $ twimage =“Images / twitter-icon.png”; //在这之前

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

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


0
投票

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

   retrive the last inserted row and fetch message and image name 

   $tweetmsg = $row['msg'];
   $image = $row['image_name'];
    $twimage = "Images/".$image;
© www.soinside.com 2019 - 2024. All rights reserved.