@@ -687,11 +687,96 @@ Here’s a sample for adding a tweet using that API method:
687
687
688
688
``` php
689
689
$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' ]
693
693
]
694
694
]);
695
695
696
696
var_dump($reply);
697
697
```
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