首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Wordpress simplexml_load_file()解析错误

Wordpress simplexml_load_file()解析错误
EN

Stack Overflow用户
提问于 2013-05-16 07:56:49
回答 1查看 1.5K关注 0票数 2

所以我遇到了一个致命的问题。我无法显示此xml数据。3个月前它还能用,但现在不行了。任何解决这个问题的建议都会对我有很大帮助!

是的-我已经检查了链接是否正常工作,并且是实时的。

代码语言:javascript
运行
复制
<?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 } ?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-16 08:42:35

我注意到最近主机对file_get_contents调用的限制越来越多。几个月前还能用的呼叫现在不能用了。

最好的解决方案是通过curl拨打电话,一旦设置好,这并不是很糟糕。

示例代码:

代码语言:javascript
运行
复制
$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将拥有该文件的内容。

您可以从托管帐户控制面板中获取服务器的绝对托管路径。用户代理并不太重要,特别是当它是您的服务器时。

棘手的部分是cert文件。你可以从以下网址获得“标准”证书:http://curl.haxx.se/docs/caextract.html

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

根据CURLOPT_CAINFO中的调用将证书复制到您的/certs目录和名称(参见上面的代码)。

您也可以将CURLOPT_SSL_VERIFYPEER更改为FALSE并不使用证书,但这将完全关闭验证,这不被认为是安全的(使用风险自负)(有关更多信息,请参阅http://php.net/manual/en/function.curl-setopt.php )。

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16576875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档