Segments do not add up to provided total file size

php

#1

Hi ,
i am trying to upload is 317kb in size & i follow video upload document of twitter & i have integrate to php twitteroauth library but i have got error "segment do not add up to provided total file size ".
This is the function i have written.

 require('twitteroauth/autoload.php');
include_once APPPATH."libraries/twitter-oauth-php-codexworld/twitteroauth.php";
$consumerKey = 'XXXXXXXXXXXX';
		$consumerSecret = 'XXXXXXXXXXXXXXXXXXXXX';
	 	 $tw_access_token = '755037110134902787-XXXXXXXXXXX';  
  	 	 $tw_secret_key = XXXXXXXXXX';  
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $tw_access_token , $tw_secret_key);
$filename = 'http://app.socialwithease.com/video/test3.mp4';
			$headers = get_headers($filename, 1);
                                $a = array(
					'command'=>'INIT',
					'total_bytes'=>$headers['Content-Length'],
					'media_type' => 'video/mp4',
					'media_category'=>'tweetvideo',
					);
                    $tweetWM = $connection->upload('media/upload', $a);	
			$fp         = fopen($img_url, 'r');
$segment_id = 0;

					while (! feof($fp)) {
					  $chunk = fread($fp, 1048576); // 1MB per chunk for this sample

					  $b = array(
						'command'=>'APPEND',
						'media_id'=>$tweetWM->media_id,
						'media'=>$img_url,					
						'segment_index' => $segment_id
						);
				$tweetWM1 = $connection->upload('media/upload', $b);	
				
					$segment_id++; 
					}
				
				$c = array(
					'command'=>'FINALIZE',
					'media_id'=>$tweetWM->media_id,
					);
				$tweetWM2 = $connection->upload('media/upload', $c);
				var_dump($tweetWM2);
             $tweet = $connection->post('statuses/update', ['media_ids' =>$tweetWM2->media_id,'status' => "Test Video Upload"]);
                    print_r($tweet);

Please tell me how to set segment index of file size.


#2

The file has to be local on your server. It can’t be a remote file with a URL.

Also TwitterOAuth has chunked uploading built in. You shouldn’t need to have your own custom while loops with INIT/APPEND/FINALIZE. Just do this:

require('twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = 'XXXXXXXXXXXX';
$consumerSecret = 'XXXXXXXXXXXXXXXXXXXXX';
$tw_access_token = '755037110134902787-XXXXXXXXXXX';  
$tw_secret_key = 'XXXXXXXXXX';  
$connection = new TwitterOAuth($consumerKey, $consumerSecret, $tw_access_token , $tw_secret_key);
$path = '/tmp/video/test3.mp4';
$options = [
  'media_type' => 'video/mp4',
  'media_category'=>'tweetvideo'
];
$media = $connection->upload($path, $options, true); // Note the third option that enables chunked uploads
$tweet = $connection->post('statuses/update', ['media_ids' =>$media->media_id,'status' => "Test Video Upload"]);