当前位置: 首页>>代码示例>>PHP>>正文


PHP HttpRequest::fetchURL方法代码示例

本文整理汇总了PHP中HttpRequest::fetchURL方法的典型用法代码示例。如果您正苦于以下问题:PHP HttpRequest::fetchURL方法的具体用法?PHP HttpRequest::fetchURL怎么用?PHP HttpRequest::fetchURL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpRequest的用法示例。


在下文中一共展示了HttpRequest::fetchURL方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: _importPhotosFromJson

 protected function _importPhotosFromJson($file)
 {
     try {
         $contents = file_get_contents($file);
         $json = JSONUtils::decode($contents);
         foreach ($json as $v) {
             echo "importing {$v->title}...";
             $url = $v->src;
             $parts = parse_url($url);
             $slug = SlugUtils::createSlug(basename($parts['path']));
             preg_match('/(\\.\\w*)$/', $parts['path'], $ext);
             $nodeRef = $this->NodeRefService->oneFromAspect('@images');
             $nodeRef = $this->NodeRefService->generateNodeRef($nodeRef, $slug);
             $node = $nodeRef->generateNode();
             if (!$this->NodeService->refExists($node->getNodeRef())) {
                 // go fetch file from url
                 $data = $this->HttpRequest->fetchURL($url);
                 // create a unique output file name
                 $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch', !empty($ext[1]) ? strtolower($ext[1]) : null);
                 file_put_contents($sourceFile, $data);
                 $node->Title = rtrim($v->title);
                 $node->Status = "published";
                 $node->ActiveDate = $this->DateFactory->newStorageDate();
                 $node = $this->ImageService->storeMedia($sourceFile, $node, basename($parts['path']));
                 $this->NodeService->add($node);
                 echo "done\n";
             } else {
                 echo "exists\n";
             }
             unset($nodeRef);
             unset($node);
         }
     } catch (Exception $e) {
         echo "Exception: " . $e->getMessage() . "\n";
     }
 }
开发者ID:wb-crowdfusion,项目名称:crowdfusion-advanced-media,代码行数:36,代码来源:MediaCliController.php

示例2: getSourceFile

 /**
  * Gets the local file from either File, Url, SourceRefNode or DataString params.
  *
  * @param array $params The user supplied parameters
  * @throws MediaServiceException if unable to fetch file
  * @return array [sourceFile], [sourceFileName]
  */
 public function getSourceFile($params)
 {
     $sourceFile = null;
     $sourceFileName = null;
     // get the actual file - check in order - uploaded file, url then node
     if (!empty($params['File'])) {
         $sourceFileName = $params['File'];
         $uploadedFiles = $params['_uploadedFiles'];
         foreach ($uploadedFiles as $uploadFile) {
             if ($uploadFile->getName() == $sourceFileName) {
                 $sourceFile = $uploadFile->getTemporaryName();
                 break;
             }
         }
         if (empty($sourceFile)) {
             throw new MediaServiceException("File '{$sourceFileName}' was not uploaded");
         }
     } else {
         if (!empty($params['Url'])) {
             // go fetch file from url
             $url = $params['Url'];
             $data = $this->HttpRequest->fetchURL($url);
             // create a unique output file name
             $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'urlfetch');
             file_put_contents($sourceFile, $data);
             $parts = parse_url($url);
             $sourceFileName = basename($parts['path']);
         } else {
             if (!empty($params['SourceNodeRef'])) {
                 // go fetch file from another node's #original
                 $node = $this->getNode($this->getNodeRef($params['SourceNodeRef']), '#original');
                 $tag = $node->getOutTag('#original');
                 if (!$tag) {
                     throw new MediaServiceException("Node '{$params['SourceNodeRef']}' has no #original tag");
                 }
                 $fileNode = $tag->getTagLinkNode();
                 $element = $node->getNodeRef()->getElement();
                 list($storageFacility, $sfParams) = $this->deriveStorageFacility($element);
                 $file = $storageFacility->getFile($sfParams, $fileNode->Title, true);
                 $contents = $file->getContents();
                 // create a unique output file name
                 $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'sffetch');
                 file_put_contents($sourceFile, $contents);
                 $sourceFileName = basename($fileNode->Title);
             } else {
                 if (!empty($params['DataString'])) {
                     $data = $params['DataString'];
                     if (preg_match('/^data:/', $params['DataString'])) {
                         $data = substr($data, strpos($data, ",") + 1);
                     }
                     if (!($file = base64_decode($data, true))) {
                         throw new MediaServiceException('Data string is not a valid base64 encoded file.');
                     }
                     // create a unique output file name
                     $sourceFile = FileSystemUtils::secureTmpname($this->workDir, 'base64fetch');
                     file_put_contents($sourceFile, $file);
                     $sourceFileName = $params['DataFileName'];
                 } else {
                     throw new MediaServiceException("No source file specified");
                 }
             }
         }
     }
     return array($sourceFile, $sourceFileName);
 }
开发者ID:wb-crowdfusion,项目名称:crowdfusion-advanced-media,代码行数:72,代码来源:MediaService.php


注:本文中的HttpRequest::fetchURL方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。