Skip to content
Advertisement

filesize(): stat failed when publishing video using twitter api | PHP

Hello I am trying to upload a video using twitter API TwitterAPIExchange.php

i have worked on the following

<?php
    // include config and twitter api wrappe
    require_once( 'config.php' );
    require_once( 'TwitterAPIExchange.php' );

    // settings for twitter api connection
    $settings = array(
        'oauth_access_token' => TWITTER_ACCESS_TOKEN, 
        'oauth_access_token_secret' => TWITTER_ACCESS_TOKEN_SECRET, 
        'consumer_key' => TWITTER_CONSUMER_KEY, 
        'consumer_secret' => TWITTER_CONSUMER_SECRET
    );


// create new twitter for api communication
    $twitter = new TwitterAPIExchange( $settings );
    
// send image to Twitter first
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';

$media_path = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/WhatCarCanYouGetForAGrand.mp4';

$postfields = array(
  "command" => "INIT",
          "total_bytes" => (int)filesize($media_path),
          'media_type' => 'video/mp4',
);

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

// get the media_id from the API return
$media_id = json_decode($response)->media_id;
$fp = fopen($media_path, 'r');
$segment_id = 0;
while (! feof($fp)) {
    $chunk = fread($fp, 1048576); // 1MB per chunk for this sample
$postfieldschunk = array(
            "command" => "APPEND",
            "media_id" => $media_id,
            'media_data' => base64_encode($chunk),
            "segment_index" => $segment_id
);
    $response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfieldschunk)
  ->performRequest();
    $segment_id++;
}
$postfieldfinal = array(
            "command" => "FINALIZE",
          "media_id" => $media_id,
);
$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfieldfinal)
  ->performRequest();
    // twitter api endpoint
    $url = 'https://api.twitter.com/1.1/statuses/update.json';
    
    // twitter api endpoint request type
    $requestMethod = 'POST';

    // twitter api endpoint data
    $apiData = array(
        'status' => 'Hello World',
          'media_ids' => $media_id,

    );


    // make our api call to twiiter
    $twitter->buildOauth( $url, $requestMethod );
    $twitter->setPostfields( $apiData );
    $response = $twitter->performRequest( true, array( CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0 ) );

    // display response from twitter
    echo '<pre>';
    print_r( json_decode( $response, true ) );
?>

but getting

filesize(): stat failed for http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/WhatCarCanYouGetForAGrand.mp4 in E:wampwwwpostblog_codetwitter_tweet_phptweet.php on line 26 I also tried in this way

what am i doing wrong here?

<?php
    // include config and twitter api wrappe
    require_once( 'config.php' );
    require_once( 'TwitterAPIExchange.php' );

    // settings for twitter api connection
    $settings = array(
        'oauth_access_token' => TWITTER_ACCESS_TOKEN, 
        'oauth_access_token_secret' => TWITTER_ACCESS_TOKEN_SECRET, 
        'consumer_key' => TWITTER_CONSUMER_KEY, 
        'consumer_secret' => TWITTER_CONSUMER_SECRET
    );
    
// send image to Twitter first
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';

$image = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4';

$postfields = array(
  'media_data' =>  base64_encode(file_get_contents($image))
);
// create new twitter for api communication
    $twitter = new TwitterAPIExchange( $settings );

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

// get the media_id from the API return
$media_id = json_decode($response)->media_id;

    // twitter api endpoint
    $url = 'https://api.twitter.com/1.1/statuses/update.json';
    
    // twitter api endpoint request type
    $requestMethod = 'POST';

    // twitter api endpoint data
    $apiData = array(
        'status' => 'This tweet is comming from an awesome script written using php and the Twitter API! #Geek #PHP #TwitterAPI',
          'media_ids' => $media_id,

    );


    // make our api call to twiiter
    $twitter->buildOauth( $url, $requestMethod );
    $twitter->setPostfields( $apiData );
    $response = $twitter->performRequest( true, array( CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0 ) );

    // display response from twitter
    echo '<pre>';
    print_r( json_decode( $response, true ) );
?>

but getting Allowed memory size of 134217728 bytes exhausted (tried to allocate 65011744 bytes) error

could anyone suggest me how can i get desired

Advertisement

Answer

You cannot stat a file via the http wrapper.

Copy the file to your server’s filesystem then read the size from the file.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement