这个话题似乎讨论了很多,但是似乎没有确定的答案。
我为Twitter的TwitterAPIExchange API开发了一个环绕类。之所以有效,是因为我可以发布文本状态,但是如果我尝试在图像中发布状态,则会显示该状态,而不显示图像。
我注意到,我首先上传图像,获取一个ID,然后将其添加到“ postfield”中。
我开发的类在本文的末尾显示,还有一个小的环绕函数使它实例化,并调用各种CTwitter函数。
产生的输出(以及var_dump的输出)如下所示。
据我所知,“ media_ids”参数只是被忽略了,我也不知道为什么。
===测试运行的输出===
声咬线束:开始 CTwitter(1.00):uploadImage:功能开始时 CTwitter(1.00):json: 字符串(169)“ {” media_id“:1035388812191399936,” media_id_string“:” 1035388812191399936“,” size“:1660220,” expires_after_secs“:86400,” image“:{” image_type“:” image / jpeg“,” w“ :4961,“ h”:3307}}“
CTwitter(1.00):res: object(stdClass)#9(5){[“” media_id“] => int(1035388812191399936)[” media_id_string“] =>字符串(19)” 1035388812191399936“ [” size“] => int(1660220)[” expires_after_secs“ ] => int(86400)[“ image”] => object(stdClass)#10(3){[“ image_type”] => string(10)“ image / jpeg” [“ w”] => int(4961 )[“ h”] => int(3307)}}
上传末尾的ID:1035388812191399936
CTwitter(1.00):uploadImage:功能结束时 从上传图片返回的ID:1035388812191399936
CTwitter(1.00):postImage:功能开始时 CTwitter(1.00):发布:功能开始时 CTwitter(1.00):后场: array(2){[“状态”] =>字符串(17)“删除此推文” [“” media_ids“] =>字符串(19)” 1035388812191399936“}
CTwitter(1.00):json: string(2457)“ {” created_at“:”星期五8月31日04:48:25 +0000 2018“,” id“:1035388817312628737,” id_str“:” 1035388817312628737“,” text“:”删除此tweet“,”被截断“:false,”实体“:{” hashtags“:[],”符号“:[],” user_mentions“:[],” urls“:[]},”源“:” \ u003ca href = \“ http ://www.xxxxxx.com/twitter \“ rel = \” nofollow \“ \ u003exxxxx \ u003c / a \ u003e”,“ in_reply_to_status_id”:null,“ in_reply_to_status_id_str”:null,“ in_reply_to_user_id_str”:nullto“ :null,“ in_reply_to_screen_name”:null,“用户”:{“ id”:180287955,“ id_str”:“ 180287955”,“名称”:“ xxxxxx”,“ screen_name”:“ xxxxxx”,“位置”:“ xxxxx “,”说明“:” xxxxx @ xxxxx / @ xxxxx。 xxxxx“,” url“:” https://t.co/xxxxx“,”实体“:{” url“:{” urls“:[{” url“:” https://t.co/ybqkAuOSbL“ ,“ expanded_url”:“ http://www.xxxxx.com”,“ display_url”:“ xxxxx.com”,“ indices”:[0,23]}]},“ description”:{“ urls”:[ ]}},“受保护”:false,“ followers_count”:1529,“ friends_count”:842,“ listed_count”:159,“ created_at”:“ Thu Aug 19 07:36:32 +0000 2010”,“ favourites_count”: 1437,“ utc_offset”:null,“ time_zone”:null,“ geo_enabled”:true,“ verified”:false,“ statuses_count”:43754,“ lang”:“ en”,“ contributors_enabled”:false,“ is_translator”: false,“ is_translation_enabled”:false,“ profile_background_color”:“ FFFFFF”,“ profile_background_image_url”:“ http://abs.twimg.com/images/themes/theme1/bg.png”,“ profile_background_image_url_https”:“ https:/ /abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/775725280002355200/q7dt7vQB_normal.jpg","profile_image_url_https “:” https://pbs.twimg.com/profile_images/775725280002355200/q7dt7vQB_normal.jpg“,” profile_banner_u rl“:” https://pbs.twimg.com/profile_banners/180287955/1484967410“,” profile_link_color“:” 0084B4“,” profile_sidebar_border_color“:” 000203“,” profile_sidebar_fill_color“:” CFDFE6“,” profile_text_color“:” 333333“,” profile_use_background_image“:true,” has_extended_profile“:false,” default_profile“:false,” default_profile_image“:false,”以下“:false,” follow_request_sent“:false,”通知“:false,” translator_type“:” none“},” geo“:null,” coordinates“:null,” place“:null,” contributors“:null,” is_quote_status“:false,” retweet_count“:0,” favorite_count“:0,” favorited“: false,“ reweeted”:false,“ lang”:“ en”}“
CTwitter(1.00):postImage:功能结束时 音咬功能:在将CTwitterSoundbite设为低级状态之后
==结束==
任何帮助/建议表示赞赏
G
=== CTwitter类===
class CTwitter {
// Member Variables
private $username; // Username of the main user
private $settings; // this holds the access tokens and the consumer keys
//private $runFromCron; // This indicates if the job was run from a cron (so $ROOT can't be (easily) established
// Twitter URLs
private $baseTwitterURL;
private $statusTwitterURL;
private $uploadTwitterURL;
private $version; // Holds the current version of this class
private $className; // Holds the name of this class
private $newline = "<br />"; // Provides easy access to a new line
private $trace; // Defines whether we want to display (possibly debugging) information
// Paths
private $ROOT;
private $paths;
// === Constructor ===
function __construct($uname, $settings_) {
$this->version = "1.00";
$this->className = "CTwitter";
$this->setTrace( true );
$this->includes();
$this->twitterURLs();
$this->username = $uname;
$this->settings = $settings_;
} // constructor
// === Destuctor ====
function __destruct() {
}
// Destuctor
private
function getNameVersion() {
return $this->getName() . " (" . $this->getVersion() . ")";
}
private
function getName() {
return $this->className;
}
private
function getVersion() {
return $this->version;
}
// Includes the various files that are required
private function includes() {
$this->ROOT = $_SERVER['DOCUMENT_ROOT'];
require_once( $this->ROOT . '/php/classes/' . 'Cpaths.php' );
$this->paths = new Cpaths();
$this->requireClass("TwitterAPIExchange");
}
private function twitterURLs() {
$this->baseTwitterURL = 'https://api.twitter.com/1.1/';
$this->statusTwitterURL = $this->baseTwitterURL . 'statuses/update.json';
$this->uploadTwitterURL = "https://upload.twitter.com/1.1/media/upload.json";
}
private function requireClass($file) {
require_once($this->paths->getPHPClasses() . $file . ".php");
}
public function setTrace($t) {
$this->trace = $t;
}
private function post($postfields) {
if ( $this->trace ) echo $this->getNameVersion() . ": post: At start of function" . $this->newline;
echo $this->getNameVersion() . ": postfields: " . $this->newline;
var_dump($postfields);
echo $this->newline . $this->newline;
$requestMethod = 'POST';
try{
$twitter = new TwitterAPIExchange($this->settings);
$json = $twitter->buildOauth($this->statusTwitterURL, $requestMethod)
->setPostfields($postfields)
->performRequest();
echo $this->getNameVersion() . ": json: " . $this->newline;
var_dump($json);
echo $this->newline . $this->newline;
return $json;
} catch (Exception $ex) {
echo $ex->getMessage();
}
if ( $this->trace ) echo $this->getNameVersion() . ": post: At end of function" . $this->newline;
}
public function postStatus($tweet) {
$postfields = array(
'status' => $tweet
);
$this->post($postfields);
} // post
public function postImage($tweet, $id) {
if ( $this->trace ) echo $this->getNameVersion() . ": postImage: At start of function" . $this->newline;
$postfields = array('status' => $tweet, 'media_ids' => $id);
$json = $this->post($postfields);
if ( $this->trace ) echo $this->getNameVersion() . ": postImage: At end of function" . $this->newline;
} // postWithImage
public function uploadImage() {
if ( $this->trace ) echo $this->getNameVersion() . ": uploadImage: At start of function" . $this->newline;
$twitter = new TwitterAPIExchange($this->settings);
$file = file_get_contents($this->ROOT . '/publications/Images/abk2011.jpg');
$data = base64_encode($file);
$method = "POST";
$params = array(
"media_data" => $data
);
$json = $twitter->setPostfields( $params )->buildOauth( $this->uploadTwitterURL, $method )->performRequest();
echo $this->getNameVersion() . ": json: " . $this->newline;
var_dump($json);
echo $this->newline . $this->newline;
// Result is a json string
$res = json_decode( $json );
echo $this->getNameVersion() . ": res: " . $this->newline;
var_dump($res);
echo $this->newline . $this->newline;
// Extract media id
$id = $res->media_id_string;
echo "ID at the end of uploadImage: " . $id . $this->newline . $this->newline;
if ( $this->trace ) echo $this->getNameVersion() . ": uploadImage: At end of function" . $this->newline;
return $id;
}
} // CTwitter class
===结束:CTwitter类===
===调用CTwitter类===
function __construct($uname) {
$this->version = "1.00";
$this->className = "CTwitterSoundbite";
$this->setTrace( false );
$this->includes();
if($this->trace) echo $this->getNameVersion() . ": __construct" . $this->newline;
$this->username = $uname;
$this->twitterDets = new CTwitterUserDetails($this->getUser());
$this->twitter = new CTwitter($this->getUser(), $this->twitterDets->getSettings());
$id = $this->twitter->uploadImage();
echo "ID returned from uploadImage: " . $id . $this->newline . $this->newline;
$this->twitter->postImage("Delete this tweet", $id);
} // constructor