CatchAction.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: NODELOG
  5. * DateTime: 2017/3/23 18:08
  6. * Description:
  7. */
  8. namespace common\modules\attachment\actions;
  9. use common\modules\attachment\models\Attachment;
  10. use yii\base\Action;
  11. class CatchAction extends Action
  12. {
  13. public $path;
  14. public $catcherFieldName = 'source';
  15. public function run()
  16. {
  17. \Yii::$app->response->format = 'json';
  18. $fieldName = $this->catcherFieldName;
  19. /* 抓取远程图片 */
  20. $list = array();
  21. $source = request($fieldName);
  22. foreach ($source as $imgUrl) {
  23. list($attachment, $error) = Attachment::uploadFromUrl($this->path, $imgUrl);
  24. array_push($list, [
  25. "state" => 'SUCCESS',
  26. "url" => $attachment["url"],
  27. "size" => $attachment["size"],
  28. "title" => htmlspecialchars($attachment["title"]),
  29. "original" => htmlspecialchars($attachment["name"]),
  30. "source" => htmlspecialchars($imgUrl)
  31. ]);
  32. }
  33. /* 返回抓取数据 */
  34. return [
  35. 'state' => count($list) ? 'SUCCESS' : 'ERROR',
  36. 'list' => $list
  37. ];
  38. }
  39. }