Jump to content

Fetch latest posts from another PW site (same server)?


Roych
 Share

Recommended Posts

Hello

Need some help "again" ?

Never done this before ...

I have three PW sites with different domains all three on the same server (each with it's own database not multisite), I would like to bring 5 latests posts from one PW sites and show them on other two sites. what would be the simplest way to do this.

I tried to follow this but just won't work it gives me blank page or 404.

https://processwire.com/blog/posts/multi-instance-pw3/#how-to-create-a-new-instance

 

Not sure if I want to use RSS feeds...

Thank you

R

Link to comment
Share on other sites

I tried to follow the RESTful API in PW? but it is to complicated for me I guess. Then I tried with https://processwire.com/blog/posts/multi-instance-pw3/ again but it's not working even on this tutorial site. Was this ever fixed or am I doing something wrong.

I would just like to show events from the main site on the second site on the same server. Similar to rss. Events are maintained only on the main site. Second site would just show the upcoming events 

I tried with: (testing)

<?php namespace ProcessWire;
// Server path to the PW installation
$path = '/home/myserver/mydomain.com/';

// The root URL for the PW installation
$url = 'http://mydomain.com/';

// Create a new ProcessWire instance
$site = new ProcessWire($path, $url);

// Find the 5 newest items in modules directory
$items = $site->pages->find("parent=/modules/, limit=5, sort=-created");

// output list
foreach($items as $item) {
  echo "
    <p>
      <a href='$item->httpUrl'>$item->title</a><br />
    </p>
    ";
}

But tracy gives me error

Spoiler

500: Internal Server Error
Excuse me… Compile Error: Cannot redeclare ProcessWire\pages() (previously declared in /home/myserver/instance2.com/wire/core/FunctionsAPI.php:63) (line 65 of /home/myserver/mydomain.com/wire/core/FunctionsAPI.php) This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged.

Any idea how to fix this? Do I have to do anything else than just put the code in my template or do I have to create an extra template for this like with (rss) or....  (Im new new to this)

Thank you very much

R

Link to comment
Share on other sites

  1. Do you have Functions API enabled?
  2. If yes to #1, if you turn off Functions API, does it work?
  3. ProcessWire version?
  4. Multilingual?
  5. Server environment (just in case)....PHP, MySQL
39 minutes ago, Roych said:

Do I have to do anything else than just put the code in my template or do I have to create an extra template for this

No. It should just work.

Link to comment
Share on other sites

not sure how to enabe/disable API functions... ?

Both sites are PW version 3.0.165

PHP Version 7.2.33

Both sites Multilingual but second language disabled for the moment (using multilingual fields)

environment PHP, MySQL

 

Thank you

R

image.png

Edited by Roych
deleted the phpinfo details
Link to comment
Share on other sites

@Roych fwiw - I've got this little test setup running fine on my local dev machine (Windows with Laragon and dummy .tld .test enabled)

<?php namespace ProcessWire;

// this runs in a folder "etc", which can be accessed locally @ http://etc.test/fetcharticles.php (no PW in this folder)
// I guess it could just as well be in the PW root of the instance from where you fetch the articles
require_once 'D:/laragon/www/pwbig/index.php'; // source PW installation root index.php
$out = '';
$site_1 = new ProcessWire('D:/laragon/www/pwbig/', 'http://pwbig.test/'); // source path / URL
$site_1_articles = $site_1->pages->get('/articles')->children;
$out .= "<div class='articles'>";
foreach($site_1_articles as $article) {
    $title = $article->title;
    $out .= "<a href='{$article->httpUrl}' title='$title'>"; // httpUrl instead of url
    $out .= "<div class='article'>";
    $out .= "<h2>$title</h2>";
    if($article->images->count) {
        $thumb = $article->images->first;
        $myImg = $thumb->size(200, 200);
        $out .= "<figure><img src='{$myImg->httpUrl}' alt='{$myImg->description}'></figure>"; // httpUrl instead of url
    }
    $out .= "</div></a>";
}
$out .= "</div>";
echo $out;

 

Link to comment
Share on other sites

 

15 hours ago, Roych said:

My entire php.info here:

Oops. You didn't need to do that, especially if there is some sensitive info there. Just needed the PHP version :-).

15 hours ago, Roych said:

not sure how to enabe/disable API functions...

The error seems to suggest that this is the issue. Hmm..

26 minutes ago, Roych said:

Not sure what to do here

  1. Have you tried on a local site? Does it work?
  2. On the remote installs, does the reverse work? i.e., accessing contents of the 'second' site inside 'main' site.
Link to comment
Share on other sites

3 hours ago, Roych said:

Thank you dragan but still not working, same error again.

Well, you can still create a page in your source PW installation that outputs either a JSON of your n latest articles, or complete with markup. On the other PW installation, just fetch that page in some way and include it in your template file, which can be as easy as file_get_contents('http://pw-site-with-articles.com.test/latest-articles.php'); No need to install and configure a full-fledged REST API module for that.

  • Like 5
Link to comment
Share on other sites

2 hours ago, dragan said:

Well, you can still create a page in your source PW installation that outputs either a JSON of your n latest articles, or complete with markup. On the other PW installation, just fetch that page in some way and include it in your template file, which can be as easy as file_get_contents('http://pw-site-with-articles.com.test/latest-articles.php'); No need to install and configure a full-fledged REST API module for that.

Omg, that easy, lol. Works perfect only had to change the url to httpUrl and everything is the way it should be. Already had file with complete makup ready.

Useful for other stuff also.

Thank you very much saved me a lot of time with this. ? 

R

Link to comment
Share on other sites

5 hours ago, kongondo said:

 

Oops. You didn't need to do that, especially if there is some sensitive info there. Just needed the PHP version :-).

I know I deleted the server paths before I posted it (I hope). Now I deleted it from the post too. ?

Thank you for all the help and your time kongondo and dragan?

R

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...