Question

I'm using the HybridAuth library. I'd like to be able to post message to my authenticated users twitter profile with images.

The setUserStatus method works well to automatically send a tweet.

I wrote the following method :

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 ) );
    }
 }

The message I get from twitter is :

Ooophs, we got an error: Update user status failed! Twitter returned an error. 403 Forbidden: The request is understood, but it has been refused.

How Can I get more precise info about error ? Does anybody allready success in sending a picture attached to a tweet ?

Thanks !

Hugo

Was it helpful?

Solution

Thanks @Heena for making myself wake up on this question, I MADE IT ;)

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 ) );
    }
}

To make it work you have to do like this :

$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 );

Enjoy !

OTHER TIPS

I did not understand it for hybridauth then I used this library https://github.com/J7mbo/twitter-api-php/archive/master.zip

Then I was successful using code below: (appears elsewhere in stack)

    <?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();
    }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top