WordPress的simplexml_load_file()解析错误 [英] Wordpress simplexml_load_file() parse error

查看:110
本文介绍了WordPress的simplexml_load_file()解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了一个致命的问题.我无法显示此xml数据.它在3个月前就开始工作了,而现在却没有.任何解决此错误的建议都会帮助我很多!

So i am having a killer issue. I can't get this xml data to show. It was working 3 months ago and now it's not. Any suggestions to fix this bugger would help me out a ton!

是-我已经检查了链接是否有效,是否正常运行.

Yes - I have checked to see if the link is working, and live and it is.

<?php 
if(file_exists('http://blog.millcitychurch.org/blog/rss.xml')){ ?>
        <h1>// THE  LATEST  FROM   MILL city</h1>

    <div class="feed">


            <?php 
            $xml = file_get_contents('http://blog.millcitychurch.org/blog/rss.xml'); 

            $url = 'http://blog.millcitychurch.org/blog/rss.xml';
            $rss = simplexml_load_file($xml);


            if($rss) {


                    $items = $rss->channel->item;
                    $i = 0;

                    foreach($items as $item) {


                        if (++$i > 4) {
                             // stop after 5 loops
                            break;
                        }



                        $title = $item->title;
                        $link = $item->link;


                        $published_on = $item->pubDate;
                        $description = strip_tags($item->description);


                        $position=215; // Define how many character you want to display.
                        $message = $description;
                        $post_content = substr($message, $position, 1);


                        $post_content = substr($message,0,$position); // Display your message


                        echo '<div class="feed-desc">' ;
                        echo '<h2>'.$title.'</h2><p>';
                        echo $post_content;
                        echo '</p><div class="readmore"><a href="'.$link.'">... read more</a></div>';
                        echo '<div class="date">'.$published_on.'</div>';
                        echo '</div>';

                    }


                } ?>



        </div><! -- end .feed -->
        <?php } ?>

推荐答案

我注意到主机最近在file_get_contents调用中变得越来越严格.几个月前起作用的呼叫现在不起作用.

I have noticed that hosts are becoming more restrictive in file_get_contents calls lately. Calls that were working a few months ago are not working now.

最好的解决方案是通过curl进行呼叫,设置好之后还算不错.

The best solution is to make your calls through curl, which is not too bad once you get it set up.

示例代码:

$htmlFile = curl('http://blog.millcitychurch.org/blog/rss.xml');


function curl($url)
{       
  //Absolute Hosting Path
  $absHostingPath = '/home/content/your_server_path/html';

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch, CURLOPT_TIMEOUT, 20); 
  curl_setopt($ch, CURLOPT_USERAGENT, array('User-Agent' => 'My User Agent'));
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
  curl_setopt($ch, CURLOPT_CAINFO, $absHostingPath."/certs/cacert.pem");

  $data = curl_exec($ch);

  $curlError = curl_error($ch);
  if(strlen($curlError) > 0)
    print(' Curl error: ' .$curlError.'  ');

   curl_close($ch);
   return $data;
}

curl()的调用返回时,

$ htmlFile将具有文件的内容.

$htmlFile will have the contents of the file when the call to curl() returns.

您可以从托管帐户控制面板中获取服务器的绝对托管路径. 用户代理不是太重要,尤其是在您的服务器上.

You can get the absolute hosting path of your server from the hosting account control panel. The user agent is not too important, especially if it is your server.

棘手的部分是证书文件.您可以从以下位置获取标准" ca证书: http://curl.haxx.se/docs/caextract.html

The tricky part is the cert file. You can get 'standard' ca certs from: http://curl.haxx.se/docs/caextract.html

如果要调用自己的服务器,则应该能够创建自己的自签名证书: http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO /x160.html

If you are calling to your own server, you should be able to create your own self-signed cert: http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/x160.html

根据CURLOPT_CAINFO中的调用将证书复制到/certs目录并命名(请参见上面的代码).

Copy the cert to your /certs directory and name according to the call in CURLOPT_CAINFO (see code above).

您还可以将CURLOPT_SSL_VERIFYPEER更改为FALSE并且不使用证书,但是这将完全关闭验证,这被认为是不安全的(使用风险自负)(请参阅

You can also change CURLOPT_SSL_VERIFYPEER to FALSE and not use a cert, but that will turn off verification altogether, which is not considered safe (use at own risk) (see http://php.net/manual/en/function.curl-setopt.php for more info).

希望有帮助!

这篇关于WordPress的simplexml_load_file()解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆