2015-01-09 268 views
0

这是我的代码,我想发布图片推文,但只有文本发布?我真的很想发布图片以及。我正在拉我的头发,帮助!!?为什么我不能通过twitter api发布图片?

<?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:\数据库\ RC_Data_FMS \图像\文件\图像\ 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; 
    } 
} 


?> 

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

回答

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 
); 

?> 

希望这有助于!