2014-07-03 20 views
0

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

 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> 

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

回答

0

这是比我想象的要容易得多,我只是用$ _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(); 
    } 
0

您必须实现小幅逻辑这里 - 现在对提交的点击就以下 1.store在你的项目的一些文件夹中的图像(请参阅下面的代码来存储图像的文件夹中)

upload_file.php //在此处上传文件代码

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

4. $ tweetmsg = $ _POST ['post_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; 

我希望这将是对你的工作..谢谢

+0

而且您的网址,HTTPS://api.twitter.com/1.1/statuses/ update_with_media.json,是错误的。它应该是:https://upload.twitter.com/1.1/statuses/update_with_media.json –

+0

感谢您的提示,我会尝试一下! – user2190027

3

接受的答案使用折旧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(); 
+0

这应该是接受答案下的评论,或者如果您有其他更新的答案,您应该编辑并包含上下文。 –

+0

我以为我会链接到另一篇文章,以避免重复,但在这里你去。 –

+0

如果答案适用,您应该将该问题标记为重复。 (不是downvoter,顺便说一句) –