Skip to content

Commit d5c79b3

Browse files
committed
Support TON API
Fix #145. This would need to be tested whether it really works.
1 parent 2780344 commit d5c79b3

File tree

3 files changed

+283
-58
lines changed

3 files changed

+283
-58
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ codebird-php - changelog
1111
+ #134 Add support for compressed remote images
1212
+ #129 Allow to change remote media download timeout
1313
+ #144 Support Collections API
14+
+ #145 Support TON API
1415

1516
2.7.2 (2015-09-23)
1617
- #135 Invalid HTTP request headers in non-cURL mode

README.md

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,96 @@ Here’s a sample for adding a tweet using that API method:
687687

688688
```php
689689
$reply = $cb->collections_entries_curate([
690-
"id" => "custom-672852634622144512",
691-
"changes" => [
692-
["op" => "add", "tweet_id" => "672727928262828032"]
690+
'id' => 'custom-672852634622144512',
691+
'changes' => [
692+
['op' => 'add', 'tweet_id' => '672727928262828032']
693693
]
694694
]);
695695

696696
var_dump($reply);
697697
```
698+
699+
### …access the TON API?
700+
701+
The [TON (Twitter Object Nest) API](https://dev.twitter.com/rest/ton) allows implementers to upload media and various assets to Twitter.
702+
The TON API supports non-resumable and resumable upload methods based on the size of the file.
703+
For files less than 64MB, non-resumable may be used. For files greater than or equal to 64MB,
704+
resumable must be used. Resumable uploads require chunk sizes of less than 64MB.
705+
706+
For accessing the TON API, please adapt the following code samples for uploading:
707+
708+
#### Single-chunk upload
709+
710+
```php
711+
// single-chunk upload
712+
713+
$reply = $cb->ton_bucket_BUCKET([
714+
'bucket' => 'ta_partner',
715+
'Content-Type' => 'image/jpeg',
716+
'media' => $file
717+
]);
718+
719+
var_dump($reply);
720+
721+
// use the Location header now...
722+
echo $reply->Location;
723+
```
724+
725+
As you see from that sample, Codebird rewrites the special TON API headers into the reply,
726+
so you can easily access them. This also applies to the `X-TON-Min-Chunk-Size` and
727+
`X-Ton-Max-Chunk-Size` for chunked uploads:
728+
729+
#### Multi-chunk upload
730+
731+
```php
732+
// multi-chunk upload
733+
$file = 'demo-video.mp4';
734+
$size_bytes = filesize($file);
735+
$fp = fopen($file, 'r');
736+
737+
// INIT the upload
738+
739+
$reply = $cb->__call(
740+
'ton/bucket/BUCKET?resumable=true',
741+
[[ // note the double square braces when using __call
742+
'bucket' => 'ta_partner',
743+
'Content-Type' => 'video/mp4',
744+
'X-Ton-Content-Type' => 'video/mp4',
745+
'X-Ton-Content-Length' => $size_bytes
746+
]]
747+
);
748+
749+
$target = $reply->Location;
750+
// something like: '/1.1/ton/bucket/ta_partner/SzFxGfAg_Zj.mp4?resumable=true&resumeId=28401873'
751+
$match = [];
752+
753+
// match the location parts
754+
preg_match('/ton\/bucket\/.+\/(.+)\?resumable=true&resumeId=(\d+)/', $target, $match);
755+
list ($target, $file, $resumeId) = $match;
756+
757+
// APPEND data to the upload
758+
759+
$segment_id = 0;
760+
761+
while (! feof($fp)) {
762+
$chunk = fread($fp, 1048576); // 1MB per chunk for this sample
763+
764+
// special way to call Codebird for the upload chunks
765+
$reply = $cb->__call(
766+
'ton/bucket/BUCKET/FILE?resumable=true&resumeId=RESUMEID',
767+
[[ // note the double square braces when using __call
768+
'bucket' => 'ta_partner',
769+
'file' => $file, // you get real filename from INIT, see above
770+
'Content-Type' => 'image/jpeg',
771+
'Content-Range' => 'bytes '
772+
. ($segment_id * 1048576) . '-' . strlen($chunk) . '/' . $size_bytes,
773+
'resumeId' => $resumeId,
774+
'media' => $chunk
775+
]]
776+
);
777+
778+
$segment_id++;
779+
}
780+
781+
fclose($fp);
782+
```

0 commit comments

Comments
 (0)