Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video from remote source #465

Closed
Jack-JohnC opened this issue May 2, 2016 · 10 comments
Closed

Video from remote source #465

Jack-JohnC opened this issue May 2, 2016 · 10 comments
Milestone

Comments

@Jack-JohnC
Copy link

Jack-JohnC commented May 2, 2016

In TwitterOAuth.php line 269 total_bytes pulls the file source locally using filesize($parameters['media']) but doesn't allow from remote sources such as a CDN without first copying the file locally.

If you allow a parameter of total_bytes, the file can be pulled from a remote source just by posting the file size in bytes.

Current: 'total_bytes' => filesize($parameters['media'])

New: 'total_bytes' => isset($parameters['total_bytes']) ? $parameters['total_bytes'] : filesize($parameters['media'])

If you want to take it further you can add in the remote file size in the API and do it automatically by specifying a remote file parameter.

    $videoUrl = 'some remote url';
    $ch = curl_init($videoUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    curl_exec($ch);
    $sizeBytes = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
    curl_close($ch);

Where $sizeBytes is total_bytes

@abraham
Copy link
Owner

abraham commented May 3, 2016

I'm not sure how this is useful. The media file has to be uploaded directly to Twitter so the file will have to be local on this server regardless.

@Jack-JohnC
Copy link
Author

I am able to upload to Twitter from a CDN without saving the file locally, but with filesize() it can't get the remote file in bytes and crashes. If I specify total_bytes with that command it lets me push the file from the cdn to Twitter.

@abraham
Copy link
Owner

abraham commented May 3, 2016

How are you doing that? Reading POST media/upload the API requires either media or media_data, both of which are the media file, not a URL a remote file.

@abraham
Copy link
Owner

abraham commented May 3, 2016

But how do you upload it to twitter?

@Jack-JohnC
Copy link
Author

Say the $videoUrl is the URL I posted:

use Abraham\TwitterOAuth\TwitterOAuth;

$twitter = $this->load_twitter();
$twitter->setTimeouts(60, 30);
$video = $twitter->upload('media/upload', ['media' => $videoUrl,'total_bytes' => '123456','media_type' => 'video/mp4'],true);
$parameters = [
    'status' => 'A video upload example',
    'media_ids' => $video->media_id_string,
];
$twitter->post('statuses/update', $parameters);

@abraham
Copy link
Owner

abraham commented May 5, 2016

Ah, I see what's happening. Twitter is not fetching the remote URL, fopen if fetching it within the TwitterOAuth code and then uploading it to Twitter. You should be downloading the file within your own code and passing a path to the TwitterOAuth upload method as documented.

I'm leaving this issue open as a task to remove unintended URL support from the upload method.

@abraham abraham added this to the v2.0 milestone Feb 23, 2017
@abraham
Copy link
Owner

abraham commented Jul 3, 2018

This might end up being a feature of #681

@abraham abraham closed this as completed Jul 3, 2018
@sanjeev1994tiwari
Copy link

this pakage is simple and easy to implement...
but i have one issue while uploading remote ( s3 file ) and gives error "filesize(): stat failed for "...
i can understand why error arise..because of s3 file url are not local path....
pls help what we do for remote url...

my code is :
$video = s3url
$media1 = $connection->upload('media/upload', ['media' => $video, 'media_type' => 'video/mp4'],true);
$parameters = [
'status' => 'video',
'media_ids' => implode(',', [$media1->media_id_string])
];
$result = $connection->post('statuses/update', $parameters);

@abraham
Copy link
Owner

abraham commented Nov 28, 2020

You'll have to download the file first. There are several approaches you can use to accomplish this. https://stackoverflow.com/questions/3938534/download-file-to-server-from-url

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants