问题
I recently asked a question, where I was struggling to get status and media to upload to Twitter, using PHP and TwitterAPIExchange.
I was given some good advice, but it still won't work. That is, I don't get an error, but the tweet does not appear.
I thought it was easier to write a new post as the previous one would not allow me to add more than a few hundred characters.
I have now re-written the function (shown at the end) so that it will ONLY tweet a status+image. This makes it shorter and, I hope, simpler to follow. I have also got rid on the deprecated call, as advised.
I have also included a dump of the key variables. These come out as:
$tweet = 'Test for Stack Exchange'
$imagesPath = '/twitter/images/GXK/'
$image = 'graham.jpg'
$url = 'https://api.twitter.com/1.1/statuses/update.json'
$imageToPost = '/twitter/images/GXK/graham.jpg'
I have also tried putting in the full path name to the image, that is:
'media[]' => "@{http://www.graham-kendall.com/twitter/images/GXK/graham.jpg}");
I have also tried:
'media' => "http://www.graham-kendall.com/twitter/images/GXK/graham.jpg");
This DOES actually tweet the text but NOT the image.
Any help appreciated.
The Function
public function postImageTweet($tweet, $imagesPath, $image) {
$url = $this->baseTwitterURL . 'statuses/update.json';
$imageToPost = $imagesPath . $image;
// Display key variables
echo "tweet: " . $tweet . $newline;
echo "imagesPath: " . $imagesPath . $newline;
echo "image: " . $image . $newline;
echo "url: " . $url . $newline;
echo "imageToPost: " . $imageToPost . $newline;
$requestMethod = 'POST';
$postfields = array(
'status' => $tweet,
'media[]' => "@{http://www.graham-kendall.com/twitter/images/GXK/graham.jpg}");
try{
$twitter = new TwitterAPIExchange($this->settings);
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
} catch (Exception $ex) {
echo $ex->getMessage();
}
} // postImageTweet
回答1:
I think $url
has to be this:
$url = $this->baseTwitterURL . 'statuses/update_with_media.json';
If you don't use 'with_media' that won't work.
来源:https://stackoverflow.com/questions/29863919/tweet-status-and-picture-with-php-using-twitterapiexchange-part-2