Unable to share image on twitter using PHP

Viewed 49

I want to share "image" on twitter with php,In other words i have image and i want whenever any user click on "share" button then image should post of his "Twitter wall", I tried with two different code/method but in both cases...image is posting on my wall (i used credentials(token,secret)with my account), so i want image should post on users(who want to share) instead of my "Twitter wall",How can i do this ? Here is my First code/method which i tried

require_once('src/codebird.php');
$image="index.jpg";
$message="Lorem Ipsum dummy text";

// note: consumerKey, consumerSecret, accessToken, and accessTokenSecret all come from your twitter app at https://apps.twitter.com/
\Codebird\Codebird::setConsumerKey("xxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxx");
$cb = \Codebird\Codebird::getInstance();
$cb->setToken("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

$reply = $cb->media_upload(array(
    'media' => $image
));

$mediaID = $reply->media_id_string;
$params = array(
    'status' => $message,
    'media_ids' => $mediaID
);
$reply = $cb->statuses_update($params);

And i tried with another code/method also, Here is my code

require("config.php");
require("vendor/autoload.php");

use Abraham\TwitterOAuth\TwitterOAuth;
$img="index.jpg";
if(isset($_POST['shares'])) {
  
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$media1 = $connection->upload('media/upload', ['media' => $img]);
$parameters = [
    'status' => 'Lorem Ipsum dummy text!',
    'media_ids' => implode(',', [$media1->media_id_string])
];
$result = $connection->post('statuses/update', $parameters);

$id=$result->id;
if($id || $id!=="")
{
    echo "Image uploaded Successfully on twitter";
}
else
{   
    echo "Error! Something went wrong";
}

}

?>
<html>
<form action="index.php" method="POST" name="myform">
<input type="hidden" name="shares" value="shares">
<input type="submit" name="submit" id="submit" value="share">
</form>
</html>
1 Answers

I use this library to post images https://github.com/dg/twitter-php ( https://phpfashion.com/twitter-for-php ):

composer.json

{
    "repositories": [
        {
            "type": "vcs",
            "url": "git@github.com:peterjaap/twitter-php.git"
        }
    ],
    "require": {
        "dg/twitter-php": "dev-patch-1"
    }
}

test.php

<?php

require_once "vendor/autoload.php";

use DG\Twitter\Twitter;

define('CONSUMER_KEY', '...your-code-here');
define('CONSUMER_SECRET', '...your-code-here');
define('ACCESS_TOKEN', '...your-code-here');
define('ACCESS_TOKEN_SECRET', '...your-code-here');

$twitter = new Twitter(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

// optional image/s
$image = "./tempfile.jpg";
file_put_contents($image, fopen("https://live.staticflickr.com/5095/5446290949_73ed733c0d_c.jpg", 'r'));

try {
    $tweet = $twitter->send('this is my cool tweet', array($image)); // you can add $imagePath or array of image paths as second argument
    unlink($image);
} catch (DG\Twitter\TwitterException $e) {
    echo 'Error: ' . $e->getMessage();
}

?>

don't forget to add write permission to your twitter app ( https://developer.twitter.com/en/portal/dashboard )

twitter write permission

Related