attachment.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * 上传下载附件示例
  4. */
  5. require_once __DIR__ . '/../autoload.php';
  6. $config = require_once 'config.php';
  7. use Easemob\Auth;
  8. use Easemob\Attachment;
  9. // 初始化授权对象,环信 token 初始化
  10. $easemob = $config['easemob'];
  11. $auth = new Auth($easemob['app_key'], $easemob['client_id'], $easemob['client_secret']);
  12. // 设置 REST 域名,沙箱环境使用,不是沙箱环境会自动获取
  13. if (isset($easemob['api_uri']) && $easemob['api_uri']) {
  14. $auth->setApiUri($easemob['api_uri']);
  15. }
  16. // 实例化对象
  17. $attachment = new Attachment($auth);
  18. echo '<pre>';
  19. /*
  20. // 上传文件
  21. $data = $attachment->uploadFile('images/1.png');
  22. var_dump($data);
  23. // array(3) {
  24. // ["uuid"]=>
  25. // string(36) "a4364f90-b0d8-11ec-8cfe-b57d5aca4e63"
  26. // ["type"]=>
  27. // string(8) "chatfile"
  28. // ["share-secret"]=>
  29. // string(48) "pDZPmrDYEeyGTyPdhVc_kj_MzWvSzIfi3bg_fgkvpVjvQAo5"
  30. // }
  31. $data = $attachment->uploadFile('/usr/share/nginx/html/sdk.com/examples/images/1.png', true);
  32. var_dump($data);
  33. // array(3) {
  34. // ["uuid"]=>
  35. // string(36) "a4615730-b0d8-11ec-92b5-1fe1ed287b6a"
  36. // ["type"]=>
  37. // string(8) "chatfile"
  38. // ["share-secret"]=>
  39. // string(48) "pGF-QLDYEeyPS6lef7QYBPQMYwHMCVRt34DkF3KYLMETh4s0"
  40. // }
  41. */
  42. /*
  43. // 下载文件
  44. var_dump($attachment->downloadFile('images/11.png', 'a4364f90-b0d8-11ec-8cfe-b57d5aca4e63'));
  45. // 下载文件
  46. var_dump($attachment->downloadFile('images/111/22.png', 'a4615730-b0d8-11ec-92b5-1fe1ed287b6a', 'pGF-QLDYEeyPS6lef7QYBPQMYwHMCVRt34DkF3KYLMETh4s0'));
  47. */
  48. /*
  49. // 下载缩略图
  50. var_dump($attachment->downloadThumb('images/11_thumb.png', 'a4364f90-b0d8-11ec-8cfe-b57d5aca4e63'));
  51. var_dump($attachment->downloadThumb('images/111/11_thumb.png', 'a4615730-b0d8-11ec-92b5-1fe1ed287b6a', 'pGF-QLDYEeyPS6lef7QYBPQMYwHMCVRt34DkF3KYLMETh4s0'));
  52. */