Php Twitter图片共享

Php Twitter图片共享,php,twitter,Php,Twitter,如何使用php在twitter上共享图像?我知道它使用twitter的图像上传api。但我不知道如何实现这一点?请帮助寻找解决方案。提前感谢 我相信上面的链接包含了所有必要的信息。这是twitter图片共享上的文档 这是唯一一个支持twitter图像上传的PHP库 下面是一个图像共享的工作示例。只需几分钟就可以编辑您的设置 <?php if ($_POST['message'] && $_FILES['image']['tmp_name']) {

如何使用php在twitter上共享图像?我知道它使用twitter的图像上传api。但我不知道如何实现这一点?请帮助寻找解决方案。提前感谢


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

这是twitter图片共享上的文档

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

下面是一个图像共享的工作示例。只需几分钟就可以编辑您的设置

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

还有一个库(它是分叉的)可用于图像上载,请参阅:

  • 下载:
  • 使用代码:
  • $path='/absolute/path/to/background.jpg';
    $meta=getimagesize($path);
    $raw['image']=$path';类型='.$meta['mime'];
    $param['tile']='true';
    $status=$connection->post($account/update\u profile\u background\u image',$param,$raw);
    
    最初,当我只提供媒体文件的路径时,我遇到了相同的错误。实际上,您需要提供字节读取。因此,请按如下方式设置媒体参数,它将正常工作:


    您可以访问此链接以获取完整参考

    谢谢!通过一些调整,这确实非常有效,但请注意,它不会与新的Twitter API 1.1 endpoint一起工作。更新:让它与API v.1.1一起工作,很抱歉我之前的评论,您需要将端点url更改为
    $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);