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


PHP URL::checkUrl方法代码示例

本文整理汇总了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);
 }
开发者ID:alex63530,项目名称:thelia,代码行数:51,代码来源:FileDownloader.php


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