Image HybridAuth发送带有图像的tweet
我正在使用HybridAuth库。 我想能够发布消息到我的认证用户twitter的个人资料与图像 setUserStatus方法可以很好地自动发送tweet 我写了以下方法:Image HybridAuth发送带有图像的tweet,image,twitter,tweets,hybridauth,Image,Twitter,Tweets,Hybridauth,我正在使用HybridAuth库。 我想能够发布消息到我的认证用户twitter的个人资料与图像 setUserStatus方法可以很好地自动发送tweet 我写了以下方法: function setUserStatus( $status, $image ) { //$parameters = array( 'status' => $status, 'media[]' => "@{$image}" ); $parameters = array( 'status' =&
function setUserStatus( $status, $image )
{
//$parameters = array( 'status' => $status, 'media[]' => "@{$image}" );
$parameters = array( 'status' => $status, 'media[]' => file_get_contents($image) );
$response = $this->api->post( 'statuses/update_with_media.json', $parameters );
// check the last HTTP status code returned
if ( $this->api->http_code != 200 ){
throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
}
}
我从twitter上得到的信息是:
哦,我们收到一个错误:更新用户状态失败!Twitter返回了一个错误。403禁止:请求已被理解,但已被拒绝
如何获得有关错误的更精确信息?
有人能成功地将图片附在推特上吗
谢谢
Hugo我不理解hybridauth,然后我使用了这个库 然后我成功地使用了下面的代码:(出现在堆栈的其他地方)
对于hybridauth,我不理解它,然后我使用了这个库 然后我成功地使用了下面的代码:(出现在堆栈的其他地方)
谢谢@Heena让我在这个问题上清醒过来,我做到了;)
享受吧 谢谢@Heena让我在这个问题上清醒过来,我做到了;)
享受吧 嘿,你修好了吗?没有!当我开始工作的时候,我会直接使用twitter API…嘿,你修好了吗?没有!当我开始工作时,我会直接使用twitter API…对消息和图像进行硬编码,就像$tweetmsg=“查看我的新照片”$twimg=“catfight.jpg”;JSON被否决,您应该考虑使用Media/Advald.JSON的新方法,然后使用STATES/UPDATET.JSOTN硬代码消息和图像,像$tWeeTMSG= =“见我的新照片”;twimg=“catfight.jpg”;JSON已被弃用,您应该考虑使用Media/Advald.JSON的新方法,然后使用STATES/UPDATET.JSON。
<?php
require_once('TwitterAPIExchange.php');
$settings= array(
'oauth_access_token' => '';
'oauth_access_secret' => '';
'consumer_key' => '';
'consumer_secret' => '';
// paste your keys above properly
)
$url_media = "https://api.twitter.com/1.1/statuses/update_with_media.json";
$requestMethod = "POST";
$tweetmsg = $_POST['post_description']; //POST data from upload form
$twimg = $_FILES['pictureFile']['tmp_name']; // POST data of file upload
$postfields = array(
'status' => $tweetmsg,
'media[]' => '@' . $twimg
);
try {
$twitter = new TwitterAPIExchange($settings);
$twitter->buildOauth($url_media, $requestMethod)
->setPostfields($postfields)
->performRequest();
echo "You just tweeted with an image";
} catch (Exception $ex) {
echo $ex->getMessage();
}
?>
function setUserStatus( $status )
{
if(is_array($status))
{
$message = $status["message"];
$image_path = $status["image_path"];
}
else
{
$message = $status;
$image_path = null;
}
$media_id = null;
# https://dev.twitter.com/rest/reference/get/help/configuration
$twitter_photo_size_limit = 3145728;
if($image_path!==null)
{
if(file_exists($image_path))
{
if(filesize($image_path) < $twitter_photo_size_limit)
{
# Backup base_url
$original_base_url = $this->api->api_base_url;
# Need to change base_url for uploading media
$this->api->api_base_url = "https://upload.twitter.com/1.1/";
# Call Twitter API media/upload.json
$parameters = array('media' => base64_encode(file_get_contents($image_path)) );
$response = $this->api->post( 'media/upload.json', $parameters );
error_log("Twitter upload response : ".print_r($response, true));
# Restore base_url
$this->api->api_base_url = $original_base_url;
# Retrieve media_id from response
if(isset($response->media_id))
{
$media_id = $response->media_id;
error_log("Twitter media_id : ".$media_id);
}
}
else
{
error_log("Twitter does not accept files larger than ".$twitter_photo_size_limit.". Check ".$image_path);
}
}
else
{
error_log("Can't send file ".$image_path." to Twitter cause does not exist ... ");
}
}
if($media_id!==null)
{
$parameters = array( 'status' => $message, 'media_ids' => $media_id );
}
else
{
$parameters = array( 'status' => $message);
}
$response = $this->api->post( 'statuses/update.json', $parameters );
// check the last HTTP status code returned
if ( $this->api->http_code != 200 ){
throw new Exception( "Update user status failed! {$this->providerId} returned an error. " . $this->errorMessageByStatus( $this->api->http_code ) );
}
}
$config = "/path_to_hybridauth_config.php";
$hybridauth = new Hybrid_Auth( $config );
$adapter = $hybridauth->authenticate( "Twitter" );
$twitter_status = array(
"message" => "Hi there! this is just a random update to test some stuff",
"image_path" => "/path_to_your_image.jpg"
);
$res = $adapter->setUserStatus( $twitter_status );