首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Twitter图片分享

Twitter图片分享
EN

Stack Overflow用户
提问于 2011-09-23 02:22:59
回答 4查看 7.9K关注 0票数 0

如何使用php在twitter中分享图片?我知道它使用的是twitter的图片上传api。但是我不知道如何实现这一点?请提前帮助查找solution.Thanks。

EN

回答 4

Stack Overflow用户

发布于 2011-09-23 04:09:06

这是Doc在twitter上的图片分享。

https://dev.twitter.com/docs/api/1/post/statuses/update_with_media

这是唯一一个支持twitter图片上传的PHP库。

https://github.com/themattharris/tmhOAuth

这是一个图像共享的工作示例。对你的集合进行几次编辑。

代码语言:javascript
复制
<?php

 if ($_POST['message'] && $_FILES['image']['tmp_name']) 
        {
                require 'tmhOAuth.php';

                list($oauth_token, $oauth_token_secret) = explode('|', $GLOBALS['user']['password']);

                $tmhOAuth = new tmhOAuth(array(
                        'consumer_key'    => OAUTH_CONSUMER_KEY,
                        'consumer_secret' => OAUTH_CONSUMER_SECRET,
                        'user_token'      => $oauth_token,
                        'user_secret'     => $oauth_token_secret,
                ));

                $image = "{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}";

                $code = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json',
                                                                                          array(
                                                                                                 'media[]'  => "@{$image}",
                                                                                                 'status'   => " " . $status //A space is needed because twitter b0rks if first char is an @
                                                                                          ),
                                                                                          true, // use auth
                                                                                          true  // multipart
                                                                                );

                if ($code == 200) {
                        $json = json_decode($tmhOAuth->response['response']);

                        if ($_SERVER['HTTPS'] == "on") {
                                $image_url = $json->entities->media[0]->media_url_https;
                        }
                        else {
                                $image_url = $json->entities->media[0]->media_url;
                        }

                        $text = $json->text;

                        $content = "<p>Upload success. Image posted to Twitter.</p>
                                                        <p><img src=\"" . IMAGE_PROXY_URL . "x50/" . $image_url . "\" alt='' /></p>
                                                        <p>". twitter_parse_tags($text) . "</p>";

                } else {
                        $content = "Damn! Something went wrong. Sorry :-("  
                                ."<br /> code=" . $code
                                ."<br /> status=" . $status
                                ."<br /> image=" . $image
                                ."<br /> response=<pre>"
                                . print_r($tmhOAuth->response['response'], TRUE)
                                . "</pre><br /> info=<pre>"
                                . print_r($tmhOAuth->response['info'], TRUE)
                                . "</pre><br /> code=<pre>"
                                . print_r($tmhOAuth->response['code'], TRUE) . "</pre>";
                }
        } ?>
票数 3
EN

Stack Overflow用户

发布于 2011-09-23 02:47:35

https://dev.twitter.com/docs/api/1/post/statuses/update_with_media

上面的链接有我相信的所有必要的信息。

票数 0
EN

Stack Overflow用户

发布于 2012-07-26 08:56:55

还有另一个库(它是派生的)可用于图像上传,请参阅:TwitterOauth

Oauth File

  • Use代码下载:

代码语言:javascript
复制
$path = '/absolute/path/to/background.jpg';
$meta = getimagesize($path);
$raw['image'] = $path.';type='.$meta['mime'];
$param['tile'] = 'true';
$status = $connection->post('account/update_profile_background_image',$param,$raw);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7519572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档