代码之家  ›  专栏  ›  技术社区  ›  Zach

将内容从一个wordpress站点拉到另一个wordpress站点

  •  -1
  • Zach  · 技术社区  · 7 年前

    我正试图找到一种在不同网站上显示网站文本的方法。

    我拥有这两个网站,它们都在wordpress上运行(我知道这可能会使它变得更困难)。我只需要一个页面来镜像页面中的文本,当原始页面更新时,镜像也会更新。

    我有一些PHP和HTML方面的经验,我也不想使用Js。 我一直在看一些建议使用cURL和file\u get\u内容的帖子,但没有成功地将其编辑到我的网站上。

    这可能吗?

    期待您的回答!

    1 回复  |  直到 7 年前
        1
  •  0
  •   dferenc ari    7 年前

    二者都 cURL file_get_contents() 可以得到 完整html输出 从url。例如,使用 file\u get\u contents() 您可以这样做:

    <?php
    
    $content = file_get_contents('http://elssolutions.co.uk/about-els');
    echo $content;
    

    但是,如果您只需要页面的一部分, DOMDocument DOMXPath 是更好的选项,对于后者,您还可以查询DOM。下面是一个示例。

    <?php
    
    // The `id` of the node in the target document to get the contents of 
    $url = 'http://elssolutions.co.uk/about-els';
    $id = 'comp-iudvhnkb';
    
    
    $dom = new DOMDocument();
    // Silence `DOMDocument` errors/warnings on html5-tags
    libxml_use_internal_errors(true);
    // Loading content from external url
    $dom->loadHTMLFile($url);
    libxml_clear_errors();
    $xpath = new DOMXPath($dom);
    
    // Querying DOM for target `id`
    $xpathResultset = $xpath->query("//*[@id='$id']")->item(0);
    
    // Getting plain html
    $content = $dom->saveHTML($xpathResultset);
    
    echo $content;