本文整理汇总了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";
}
}
示例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);
}