Jump to content

How to use Json generated from Pages Web Service module


manlio
 Share

Recommended Posts

Hi, I would like to use data from a PW website (a list of books with cover) on another PW website (on different server and domains) just for convenience because these data are identical but are presented differently. I have installed the Pages Web Service module and it generates correct output.

Now, sorry for my ignorance, but I would like to know how I can read the generated Json. Is there something built in processwire? I should search something in PHP?

Probably I searched wrong terms but I wasn't able to find anything useful to me.

Thank you!

Link to comment
Share on other sites

If you need to query this from the template file via php, I think you could use cURL library. You get you json output and decode it with json_decode. Then you build the desired output with regular php. Probably there is a way to make a temporary PageArray from the received data for the ability to use standard API calls, but I never done it (as never done other things suggested above, so please be critical and share you experience).

  • Like 1
Link to comment
Share on other sites

If your hoster allows it you can even use something like:

<?php
// fetch the json displayed on the service page
$dataJson = file_get_contents('http://url.com/service-page/');

// transform it into a php array
$data = json_decode(dataJson, true);
?>

If this shouldn't work you can use file_get_contents_curl (not default PHP function but you can find it here: http://stackoverflow.com/a/8543512/4044167) instead of file_get_contents()

  • Like 2
Link to comment
Share on other sites

Thank to everybody for your support!

At the end Nico suggestion's worked like a charm and luckily I have no security limitations on the server.

I report here the code I finally use and I hope this could be useful to someone else

// fetch the json displayed on the service page
$dataJson = file_get_contents('http://www.serviceurl.com/service-pages/?parent=1145');

// transform it into a php array
$items = json_decode($dataJson, true);

foreach ($items['matches'] as $item) { // start the looping. "matches" is the name of the results array generated by the webservice

echo $item['title'];
echo $item['body'];
echo 'http://www.myurl.com/site/assets/files/' . $item['id'].'/'.$item['myimage']['basename']; // url image
}

If you need to pass an image, the last line rebuild the url and Insted of www.myurl.com and  [myimage] field you have to write your own and than you can use in an img tag.

  • Like 2
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...