Php 职位状态+;Twitter上带有TwitterAPIExchange的图像

Php 职位状态+;Twitter上带有TwitterAPIExchange的图像,php,twitter,Php,Twitter,我有下面的工作代码,代码只是在我的twitter页面上发布一个状态和一个图像 require_once('TwitterAPIExchange.php'); $settings = array( 'oauth_access_token' => "xxx", 'oauth_access_token_secret' => "xxx", 'consumer_key' => "xxx

我有下面的工作代码,代码只是在我的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>

提交

你们有什么建议或指南可以帮我解决问题吗?我是用正确的方式思考问题,还是应该用另一种方式解决问题?谢谢

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

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

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

四,。 $tweetmsg=$\u POST['POST\u text']; $twimage=“Images/twitter icon.png”//在此之前

   retrive the last inserted row and fetch message and image name 

   $tweetmsg = $row['msg'];
   $image = $row['image_name'];
    $twimage = "Images/".$image;

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

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

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

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

四,。 $tweetmsg=$\u POST['POST\u text']; $twimage=“Images/twitter icon.png”//在此之前

   retrive the last inserted row and fetch message and image name 

   $tweetmsg = $row['msg'];
   $image = $row['image_name'];
    $twimage = "Images/".$image;

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

这比我想象的要简单得多,我只是用$\u文件将图像存储在一个变量中,这样我就可以在$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();
    }

这比我想象的要容易得多,我只是使用$\u文件将图像存储在变量中,以便在$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();
    }

接受的答案使用折旧后的API端点

以下是一个可行的解决方案:

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

接受的答案使用折旧后的API端点

以下是一个可行的解决方案:

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

您的URL也是错误的。应该是:谢谢你的提示,我会试试的!您的URL也是错误的。应该是:谢谢你的提示,我会试试的!这应该是在接受的答案下的评论,或者如果你有另一个更为最新的答案,你应该编辑并包含上下文。我想我应该链接到另一个帖子以避免重复,但现在开始。如果答案适用,你应该将问题标记为重复。(不是downvoter,顺便说一句)我已经删除了另一篇帖子上的答案,并将其标记为重复,因为这是旧的。这应该集成在TwitterApiExchange中。这应该是已接受答案下的评论,或者如果你有另一个更新的答案要提供,你应该编辑并包含上下文。我想我应该链接到另一篇文章以避免重复,但现在你可以了。如果答案适用,你应该将问题标记为重复。(顺便说一句,不是downvoter)我已经删除了另一篇帖子上的答案,并将其标记为重复,因为这是旧的。这应该集成到TwitterAPIExchange中