为什么我不能通过twitter api发布图片?

时间:2015-01-09 22:35:14

标签: api twitter

  

这是我的代码,我试图用图片发布推文,但只发布文字?我真的很想发布图片。我正在拉我的头发,帮助!!?

<?php
error_reporting(E_ALL);
require_once('TwitterAPIExchange.php');
//echo 'start';
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
require_once('connect.php');
$recid=$_GET['recid'];
//echo $recid;

$dsn='mysql:host='.$hostname.';dbname=twitter_db';
try{
    $dbh=new PDO($dsn,$username,$password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $stmt=$dbh->prepare("SELECT * FROM gr_twitter WHERE recid=:recid");
    $stmt->execute(array(
                    'recid'=>$recid
                    ));
    $foundResult=$stmt->fetchAll();
    $tweetmsg=$foundResult[0]['tweet'];
    $tweetImage=$foundResult[0]['tweetImageName'];
    $timestamp=$foundResult[0]['sentTimestamp'];

    print_r($foundResult);
    $stmt2=$dbh->prepare("UPDATE gr_twitter SET sent=1 WHERE recid=:recid");
    $stmt2->execute(array(
                    'recid'=>$recid
                    ));
    }
catch(PDOException $e){}
// Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod='POST';

////图像存储在D:\ Databases \ RC_Data_FMS \ images \ Files \ images \ tweetImage文件夹中

    $tweetImage='D:\Databases\RC_Data_FMS\images\Files\images\tweetImage/images.jpg';
        $postfields = array(
            'status' => $tweetmsg,
            'media' => "@{$tweetImage}"

        );
/** POST fields required by the URL above. See relevant docs as above **/
//print_r($postfields).'<br />';
            $twitter = new TwitterAPIExchange($Yh_settings);
  $response=  $twitter->buildOauth($url, $requestMethod)
                    ->setPostfields($postfields)
                    ->performRequest();

            echo "Success, you just tweeted!<br />";


var_dump(json_decode($response));
//////////////////////////////////////////////////////////////////////////
function objectToArray($d) 
{
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        /*
        * Return array converted to object
        * Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
       // return array_map(__FUNCTION__, $d);
    } else {
        // Return array
       // return $d;
    }
}


?>

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////////////

1 个答案:

答案 0 :(得分:0)

如果您要发布位于服务器中的图片,建议您使用tmhOAuth library

这里有一个例子:

<?php

require_once('./tmhOAuth.php');

$tmhOAuth = new tmhOAuth(array(
    'consumer_key'    => CONSUMER_KEY,
    'consumer_secret' => CONSUMER_SECRET,
    'user_token'      => OAUTH_TOKEN,
    'user_secret'     => OAUTH_TOKEN_SECRET
));

$tweetText = 'Your text here';
$imageName = 'picture.png';
$imagePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $imageName;

$code = $tmhOAuth->request(
    'POST',
    $tmhOAuth->url('1.1/statuses/update_with_media'),
    array(
        'media[]'  => "@{$imagePath};type=image/png;filename={$imageName}",
        'status'   => $tweetText
    ),
    true, // use auth
    true  // multipart
);

?>

希望这有帮助!