开发者

Twitter image sharing [closed]

开发者 https://www.devze.com 2023-04-06 15:11 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist

Closed 9 years ago.

Improve this question

How to share images in twitter using php? I know its using image upload api of twi开发者_开发技巧tter. But i have no idea how to implement this ? Please help to find a solution.Thanks in advance.


Here's the Doc on twitter image sharing .

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

Here's the only PHP library that supports twitter image uploads.

https://github.com/themattharris/tmhOAuth

And Here's a working example of image sharing. With a couple edits your set.

<?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>";
                }
        } ?>


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

Above link has all the necessary information I believe.


There is one other library (it is forked) that can be used for image upload, see: TwitterOauth

  1. Download: Oauth File
  2. Use code:
$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);


Initially I got the same error when I simply provided the path of media file. Actually you need to provide the byte read. So set your media parameter as follows and it will work:

$filename = "image.jpg";
$handle = fopen($filename, "rb");
$image = fread($handle, filesize($filename));
fclose($handle);


$params = array('media[]' => "{$image};type=image/jpeg;filename={$filename}" ,
            'status'   => 'my status');
$response =$twitteroauth->post('statuses/update_with_media', $params,true,true);

You can visit this link for complete reference http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/

0

精彩评论

暂无评论...
验证码 换一张
取 消