2014-02-26 22 views
0

我想在twitter上發佈狀態和圖片。它是twitting狀態,但不是圖像。推文狀態,但不是圖像

的鳴叫我的PHP代碼是

<?php 
session_start(); 
require_once('twitteroauth/twitteroauth.php'); 
require_once('config-sample.php'); 

if (isset($_REQUEST['oauth_token']) && $_SESSION['oauth_token'] !== $_REQUEST['oauth_token']) { 
    $_SESSION['oauth_status'] = 'oldtoken'; 
    header('Location: ./clearsessions.php'); 
} 

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); 


$access_token = $connection->getAccessToken($_REQUEST['oauth_verifier']); 

$_SESSION['access_token'] = $access_token; 

unset($_SESSION['oauth_token']); 
unset($_SESSION['oauth_token_secret']); 

if (200 == $connection->http_code) { 

    $_SESSION['status'] = 'verified'; 
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']); 

$content = $connection->get('account/verify_credentials'); 

$image='abc.jpg'; 

$image1=file_get_contents($image); 

$array_disp[0]=$image; 
$connection->post('statuses/update',array('status' =>'hello123', 'media[]'=>$image1)); 

$response=$connection->response['response']; 

} else { 

    header('Location: ./clearsessions.php'); 
} 

現在,如何利用媒體帖子,上傳圖片?

回答

0

我猜你正在使用Abraham's庫。它不適用於upload_with_media。下面是對這個問題

TwitterOAuth does not currently support media uploads. I hope to add support in the future

替代方法他的發言只是從這個library下載twitteroauth文件夾並將其粘貼到您的項目爲twitteroauth1。現在,當您要使用媒體發送推文時,請致電require_once('twitteroauth1/twitteroauth.php');

(或)

您可以簡單地複製twitteroauth目錄我們已經下載並在您的項目替換它。現在,你的代碼應該是require_once('twitteroauth/twitteroauth.php');,因爲我們替換了目錄。這應該工作!

<?php 
session_start(); 
require_once('twitteroauth1/twitteroauth.php'); 
require_once('config-sample.php'); 
if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token'])  || empty($_SESSION['access_token']['oauth_token_secret'])) { 
header('Location: ./clearsessions.php'); 
} 

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET,$_SESSION['access_token'] ['oauth_token'], $_SESSION['access_token']['oauth_token_secret']); 
$image='abc.jpg'; 
$content = $connection->get('account/verify_credentials'); 
$params = array('media[]' => "@{$image}", 'status' => "Testing upload"); 
$connection->post('statuses/update_with_media', $params,true); 
echo "Picture has uploades successfully"; 
?> 
相關問題