Geen omschrijving

rs_asynch_fetch.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. require_once __DIR__ . '/../autoload.php';
  3. use Qiniu\Auth;
  4. use Qiniu\Config;
  5. use Qiniu\Storage\BucketManager;
  6. // 控制台获取密钥:https://portal.qiniu.com/user/key
  7. $accessKey = getenv('QINIU_ACCESS_KEY');
  8. $secretKey = getenv('QINIU_SECRET_KEY');
  9. $bucket = getenv('QINIU_TEST_BUCKET');
  10. $auth = new Auth($accessKey, $secretKey);
  11. $config = new Config();
  12. $config->useHTTPS = true; // 接口是否使用 HTTPS 协议
  13. $bucketManager = new BucketManager($auth, $config);
  14. // 异步第三方资源抓取
  15. // 参考文档:https://developer.qiniu.com/kodo/api/4097/asynch-fetch
  16. // 需要抓取的文件 URL
  17. $url = 'http://devtools.qiniu.com/qiniu.png';
  18. //回调 URL(需要可以公网访问,并能够相应 200 OK)
  19. $callbackurl = "http://your.domain.com/upload_verify_callback.php";
  20. // 回调Body
  21. $callbackbody = '{"key":"$(key)","hash":"$(etag)","w":"$(imageInfo.width)","h":"$(imageInfo.height)"}';
  22. //---------------------------------------- demo1 ----------------------------------------
  23. // 指定抓取的文件保存到七牛云空间中的名称
  24. $key = time() . '.png';
  25. list($ret, $err) = $bucketManager->asynchFetch($url, $bucket, null, $key, null, null, $callbackurl, $callbackbody);
  26. echo "=====> asynch fetch $url to bucket: $bucket key: $key\n";
  27. if ($err !== null) {
  28. var_dump($err);
  29. } else {
  30. $id = $ret['id'];
  31. echo "id is: $id\n";
  32. }
  33. //---------------------------------------- demo2 ----------------------------------------
  34. // 不指定 key 时,以文件内容的 hash 作为文件名
  35. $key = null;
  36. list($ret, $err) = $bucketManager->asynchFetch($url, $bucket, null, $key, null, null, $callbackurl, $callbackbody);
  37. echo "=====> asynch fetch $url to bucket: $bucket key: $(etag)\n";
  38. if ($err !== null) {
  39. var_dump($err);
  40. } else {
  41. $id = $ret['id'];
  42. echo "id is: $id\n";
  43. }
  44. // 查询异步抓取的进度和状态
  45. // 华东:z0,华北:z1,华南:z2,北美:na0,东南亚:as0
  46. $zone = 'z2';
  47. sleep(10); // 由于异步抓取需要耗时,等待 10 秒后再查询状态
  48. list($ret, $err) = $bucketManager->asynchFetchStatus($zone, $id);
  49. echo "\n====> asynch fetch status: \n";
  50. if ($err != null) {
  51. var_dump($err);
  52. } else {
  53. var_dump($ret);
  54. }