检查 url 是否有效以及 php 中的 XML 是否有效

发布于 2024-12-14 02:43:47 字数 2162 浏览 0 评论 0原文

我想读取 rss feed 并存储它。为此我正在使用:-

<?php
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
 $xml = simplexml_load_string($homepage);
 echo '<pre>';
 print_r($xml);
 ?>

但首先我想检查

1.URL 是否有效,意味着它的响应时间

   $homepage = file_get_contents('http://www.forbes.com/news/index.xml');

是否少于 1 分钟并且 url 地址是正确

2.然后检查文件(http://www.forbes.com/news/index.xml)是否有有效的XML数据。 如果 XML 有效,则显示响应时间,否则显示错误。

我的问题的回答:

感谢大家的帮助和建议。我解决了这个问题。为此我写了这段代码

  <?php
 // function() for valid XML or not
 function XmlIsWellFormed($xmlContent, $message) {
libxml_use_internal_errors(true);

$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xmlContent);

$errors = libxml_get_errors();
if (empty($errors))
{
    return true;
}

$error = $errors[ 0 ];
if ($error->level < 3)
{
    return true;
}

$lines = explode("r", $xmlContent);
$line = $lines[($error->line)-1];

$message = $error->message . ' at line ' . $error->line . ': ' . htmlentities($line);

return false;
 }
   //function() for checking URL is valid or not
  function Visit($url){
   $agent = $ch=curl_init();
   curl_setopt ($ch, CURLOPT_URL,$url );
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch,CURLOPT_VERBOSE,false);
   curl_setopt($ch, CURLOPT_TIMEOUT, 60);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch,CURLOPT_SSLVERSION,3);
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   $page=curl_exec($ch);
   //echo curl_error($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   if($httpcode>=200 && $httpcode<300) return true;
   else return false;
  }
         $url='http://www.forbes.com/news/index.xml';
         if (Visit($url)){
   $xmlContent = file_get_contents($url);

      $errorMessage = '';
      if (XmlIsWellFormed($xmlContent, $errorMessage)) {
      echo 'xml is valid';
        $xml = simplexml_load_string($xmlContent);
        echo '<pre>';
        print_r($xml);
      }

     }



 ?>

I'm wanted to read a rss feed and store it.for this I m using:-

<?php
$homepage = file_get_contents('http://www.forbes.com/news/index.xml');
 $xml = simplexml_load_string($homepage);
 echo '<pre>';
 print_r($xml);
 ?>

but first I want to check

1.URL is valid or not ,means if its response time of

   $homepage = file_get_contents('http://www.forbes.com/news/index.xml');

is less than 1 minutes and the url address is correct

2.Then check the File(http://www.forbes.com/news/index.xml) have a valid XML data or not.
if valid XML then show response time else show error.

answer Of MY QUESTION:

Thanks everybody for your help and suggestion.I solved this problem. for this I wrote this code

  <?php
 // function() for valid XML or not
 function XmlIsWellFormed($xmlContent, $message) {
libxml_use_internal_errors(true);

$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xmlContent);

$errors = libxml_get_errors();
if (empty($errors))
{
    return true;
}

$error = $errors[ 0 ];
if ($error->level < 3)
{
    return true;
}

$lines = explode("r", $xmlContent);
$line = $lines[($error->line)-1];

$message = $error->message . ' at line ' . $error->line . ': ' . htmlentities($line);

return false;
 }
   //function() for checking URL is valid or not
  function Visit($url){
   $agent = $ch=curl_init();
   curl_setopt ($ch, CURLOPT_URL,$url );
   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt ($ch,CURLOPT_VERBOSE,false);
   curl_setopt($ch, CURLOPT_TIMEOUT, 60);
   curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
   curl_setopt($ch,CURLOPT_SSLVERSION,3);
   curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
   $page=curl_exec($ch);
   //echo curl_error($ch);
   $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   curl_close($ch);
   if($httpcode>=200 && $httpcode<300) return true;
   else return false;
  }
         $url='http://www.forbes.com/news/index.xml';
         if (Visit($url)){
   $xmlContent = file_get_contents($url);

      $errorMessage = '';
      if (XmlIsWellFormed($xmlContent, $errorMessage)) {
      echo 'xml is valid';
        $xml = simplexml_load_string($xmlContent);
        echo '<pre>';
        print_r($xml);
      }

     }



 ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘东忘西忘不掉你 2024-12-21 02:43:47

如果 url 无效,file_get_contents 将失败。

检查 xml 是否有效 如果

simplexml_load_string(file_get_contents('http://www.forbes.com/news/index.xml'))

有效则返回 true,如果无效则完全失败。

 if(simplexml_load_string(file_get_contents('http://www.forbes.com/news/index.xml'))){

        echo "yeah";
    }else { echo "nah";}

If the url is not valid file_get_contents would fail.

To check if the xml is valid

simplexml_load_string(file_get_contents('http://www.forbes.com/news/index.xml'))

That would return true if its and would fail entirely if it isn't.

 if(simplexml_load_string(file_get_contents('http://www.forbes.com/news/index.xml'))){

        echo "yeah";
    }else { echo "nah";}
甜是你 2024-12-21 02:43:47

页面有一个带有验证器的代码片段对于使用正则表达式的 URL。功能及用途:

function isValidURL($url)
{
     return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

if(!isValidURL($fldbanner_url))
{
    $errMsg .= "* Please enter valid URL including http://<br>";
}

This page has a snippet with a validator for a URL using regular expressions. The function and usage:

function isValidURL($url)
{
     return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

if(!isValidURL($fldbanner_url))
{
    $errMsg .= "* Please enter valid URL including http://<br>";
}
迟月 2024-12-21 02:43:47
if (!filter_var('anyurl',FILTER_VALIDATE_URL))
 echo "Wrong url";
end;

http://php.net/manual/en/filter.filters.validate.php

if (!filter_var('anyurl',FILTER_VALIDATE_URL))
 echo "Wrong url";
end;

http://php.net/manual/en/filter.filters.validate.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文