Twitter上传媒体 - 无法验证您的身份

时间:2014-12-15 14:52:39

标签: php twitter twitter-oauth

尝试将照片上传到Twitter时出现问题。我使用twitteroauth.php进行身份验证,我通过api发布推文或接收数据没有问题。但是当我想上传照片时,我收到以下错误:{"错误":[{"消息":"无法验证您",&# 34;代码" 32}]}

这是我的代码:

<?php
require_once('../include/initialize.php');

require_once('twitter/twitteroauth.php');

if(isset($_GET['access_token']) && $_GET['access_token'] != ''){
  $accesstoken = $_GET['access_token'];
}else{
  $accesstoken = TWITTER_ACCESS_TOKEN;
}

if(isset($_GET['access_token_secret']) && $_GET['access_token_secret'] != ''){
  $accesstokensecret = $_GET['access_token_secret'];
}else{
  $accesstokensecret = TWITTER_ACCESS_TOKEN_SECRET;
}

$consumerkey = TWITTER_CONSUMER_KEY;
$consumersecret = TWITTER_CONSUMER_SECRET;
$twitter = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$url = $_POST['url'];
$filename = $_POST['filename'];

// function to replace file_get_contents()
function file_get_the_contents($url) {
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}

// original script updated with new function
$imgget = file_get_the_contents($url);

$twitter_api = "https://upload.twitter.com/1.1/media/upload.json?media=".$imgget."&type=photo";

$twitter_img = $twitter->post($twitter_api);

echo json_encode($twitter_img);

任何人都可以帮我解决这个问题。我在论坛上浏览过,但我发现的所有其他问题都是指不推荐使用的status / update_with_media。 感谢。

1 个答案:

答案 0 :(得分:1)

根据thistoster-cx答案,您需要确保以64为基数对图像数据进行编码。否则,Twitter将以错误32进行响应。您将需要以下内容:

$imgget = base64_encode(file_get_the_contents($url));