本文整理汇总了PHP中Thelia\Tools\URL::checkUrl方法的典型用法代码示例。如果您正苦于以下问题:PHP URL::checkUrl方法的具体用法?PHP URL::checkUrl怎么用?PHP URL::checkUrl使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Thelia\Tools\URL
的用法示例。
在下文中一共展示了URL::checkUrl方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: download
/**
* @param string $url
* @param string $pathToStore
* @throws \Thelia\Exception\FileNotFoundException
* @throws \ErrorException
* @throws \HttpUrlException
*
* Downloads the file $url in $pathToStore
*/
public function download($url, $pathToStore)
{
if (!URL::checkUrl($url)) {
/**
* The URL is not valid
*/
throw new HttpUrlException($this->translator->trans("Tried to download a file, but the URL was not valid: %url", ["%url" => $url]));
}
/**
* Try to get the file if it is online
*/
$con = curl_init($url);
curl_setopt($con, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($con);
$errno = curl_errno($con);
$curlErrorMessage = curl_error($con);
$httpCode = curl_getinfo($con, CURLINFO_HTTP_CODE);
curl_close($con);
if (false === $response || $errno !== 0 || $httpCode != "200" && $httpCode != "204") {
/**
* The server is down ? The file doesn't exist ? Anything else ?
*/
$errorMessage = $this->translator->trans("cURL errno %errno, http code %http_code on link \"%path\": %error", ["%errno" => $errno, "%path" => $url, "%error" => $curlErrorMessage, "%http_code" => $httpCode]);
$this->logger->error($errorMessage);
throw new FileNotFoundException($errorMessage);
}
/**
* Inform that you've downloaded a file
*/
$this->logger->info($this->translator->trans("The file %path has been successfully downloaded", ["%path" => $url]));
/**
* Then try to write it on the disk
*/
$file = @fopen($pathToStore, "w");
if ($file === false) {
$translatedErrorMessage = $this->translator->trans("Failed to open a writing stream on the file: %file", ["%file" => $pathToStore]);
$this->logger->error($translatedErrorMessage);
throw new \ErrorException($translatedErrorMessage);
}
fputs($file, $response);
fclose($file);
}