Jump to content

Pages Web Service (ServicePages)


ryan

Recommended Posts

I'm trying out the module to use it as a web-service for a mobile app. I'm querying pages that have images and attachments and the returning JSON output lists the "basename" but no URL or any other way to find the asset. Is there something missing? Should I need to query something else?

Link to comment
Share on other sites

I had the same issue and hard-coded the url pointing to the asset file (using the id and basename fields)... Actually, it seemed like a workaround to me. Getting a url from the module would be much better, but I couldn't firgure out how to do that :-(

Link to comment
Share on other sites

Is there somebody with experience with the multilanguage fields in modules ?

I tried to give the language as a GET var and chaging the user object language var, but no success

I recently wrote a tiny little frontend template script for querying a product catalogue, to get JSON results back (i.e. not a module - have just discovered the module and this forum thread today). It's supposed to feed an iOS app, sooner or later. 
 
As expected from working with PW, it was all easy and straightforward.
 
Since my site in querstion is also using multilang features (4 languages, PW 2.3.2 version), I simply query
german language data via domain.com/de/API/?foo=bar?etc.etc.
and french data with domain.com/fr/API/?foo=bar?etc.etc.
 
But it would be relatively easy to output all four languages with the same "web service" URL, if really needed. I guess something like this should be all that's needed:
 
$user->language = $languages->get("en"); // the following selector query will return english
$user->language = $languages->get("fr"); // the following selector query will return french
$user->language = $languages->get("default"); // the following selector query will return default lang.
 
And if you only want to use ONE service page URL, and decide to just get ONE language back, simply use another GET param (〈=en) and a switch / if-else statement in your code.
  • Like 1
Link to comment
Share on other sites

I'm trying out the module to use it as a web-service for a mobile app. I'm querying pages that have images and attachments and the returning JSON output lists the "basename" but no URL or any other way to find the asset. Is there something missing? Should I need to query something else?

Have a look at the $config stuff here: http://cheatsheet.processwire.com/ (e.g. $config->httpHost)

Link to comment
Share on other sites

I had the same issue and hard-coded the url pointing to the asset file (using the id and basename fields)... Actually, it seemed like a workaround to me. Getting a url from the module would be much better, but I couldn't firgure out how to do that :-(

I didn't realize this aspect of the file fields output. I've put this on my to-do list for the next version of the module (actually for ProcessPageSearch, which is what this module uses). 

  • Like 2
Link to comment
Share on other sites

  • 2 months later...

Hi Ryan, 

I'm trying to use various operators in Pages Web Service, but some of the operators do not seem to be working. 

^= Contains the exact word or phrase at the beginning of the field 
The key-value pairs using this operator are completely ignored. The [selector] part of the response only includes other key-value pairs.

request:  ?template=industries&title^=Tire&debug=1

response: [selector] => template=industries, limit=50

...I tried with '%=' and it works fine:

request:  ?template=industries&title%=Tire&debug=1

response: [selector] => template=industries, title%=Tire, limit=50

...also with *= works fine:

request:  ?template=industries&title*=Tire&debug=1

response: [selector] => template=industries, title*=Tire, limit=50

and, of course '=' works:

 

request:  ?template=industries&title=Tire Manufacturing&debug=1

response: [selector] => template=industries, title=Tire Manufacturing, limit=50

 

 

$= Contains the exact word or phrase at the end of the field 
The key-value pairs using this operator are completely ignored. The [selector] part of the response only includes other key-value pairs. (as above)

~= Contains all the words 

Always throws an error because that field name is not searchable . The ‘~’ symbol is converted to ‘_’, is considered part of the field name and not treated as an operator. 

request: ?template=industries&title~=Tire&debug=1

response (error):   Field 'title_' is not allowed in queries

... I tried disabling the sanitizing of the field name before the error is displayed (line 189), and it then shows the error with the field name being  title~ 

request: ?template=industries&title~=Tire&debug=1

response (error):   Field 'title~' is not allowed in queries

 

I tried looking through the module, but I'm not seeing the cause of the problem. Any help would be greatly appreciated!

Thanks,

-Brent

Link to comment
Share on other sites

Hy guys,

i'm using Page Web Service module and everything is fine.

I'm querying for a template that has a repeater page field in it. 

I get json results with the array node for the repeater field. Every child of this array has these fields from the page:

- id

- name
- parent_id
- path
- template
- title
 
Is there a way to get other fields from that page?
 
Thanks....i hope my english is clear enough.
 
EDIT:
 
I've the same problem with the image field. I get the basename of the image but i don't get the path, so i don't know how top put it in my page. 
Link to comment
Share on other sites

  • 2 months later...

If you use jQuery, you could try a little workaround to display your images. 

Not a very elegant way, but should work for small projects.

I started with a little php function to get my newsposts and generate the markup:

function getNewsfeed(){
  $curl = curl_init();
  curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://example.com/service-pages/?template=news&sort=-modified&limit=5'
  ));
  $result = curl_exec($curl);
  curl_close($curl);
  $data = '';
  $result = json_decode($result, true);
  if($result){
    foreach($result[matches] as $news){
      $data .='<div class="js-newsfeedentry s-newsfeedentry">';
      $data .='<h3>'.$news[title].'<small class="pull-right text-muted" style="padding-top:5px">Posted : '.date("d.m.Y - H:i",$news[created]).'</small></h3>';
      $data .= $news[text];
      $data .='<hr />';
      $data .='</div>';
    }
  }
  return $data;
}

as you can see, I wrap the entire newspost into a div with the class "js-newsfeed".

Now we iterate over all images inside this div and manipulate the img src, to match the correct url from the web-service.

Inside our document ready function we grab all news images and correct the src:

 /* fix urls for newsfeed img */
  var newsfeedimg = $('.js-newsfeed img');
  newsfeedimg.each(function(){
    var newimgsrc = 'http://example.com'+$(this).attr('src');
    $(this).attr('src',newimgsrc);
  });

et voila, working images. 

Remember that this isn´t a very good solution because you iterate over every image in your newspost via JavaScript and you shouldn´t do this on a project with many images. 

  • Like 1
Link to comment
Share on other sites

  • 3 weeks later...

Hi everyone,

I try to test this module and it seems it does not work when I try the examples in the service-pages page that comes with this module. I have made sure that the template field is included in the config but it comes up with this error: Error: Template 'basic-page' is not allowed in queries

Not quiet sure what settings I did miss here. Here is the screenshot:

zBbexhl.png

Appreciate any help to solve this issue :) Cheers

Link to comment
Share on other sites

  • 5 weeks later...

This module works great when I'm logged in as a superuser, when when logged out, I get the following error instead of viewing the JSON output:

Error: 	Exception: You do not have permission to execute this module - ProcessPageSearch (in /home/[...]/public_html/wire/core/Modules.php line 458)

This error message was shown because you are using the command line API. Error has been logged. 

I'm on 2.4.3.

Link to comment
Share on other sites

Hi jacmaes,

I have tracked down the issue. It is definitely a bug that should be reported to Ryan on Github.

There are newly added checks for module permissions around line#457 of Modules.php (https://github.com/ryancramerdesign/ProcessWire/commit/2c0762d1f346a3f01171b4f65fa97242f17fd916#diff-995c5c9ce48d4bff459f52363c6e190eR457)

The module that is blocking your access is actually ProcessPageSearch, which required page-edit permissions, which of course a guest user doesn't have.

If you temporarily remove the permission check, or change the ProcessPageSearch permissions to page-view, it works again (obviously this is not the appropriate solution :).

Once that hack is in place though, there is one other new bug:

{"errors":["Field 'it' is not allowed in queries"]}

This can be fixed easily by adding this line to ServicePages.module inside the loop starting at line#174

foreach($k as $name) {
   if($name=="it") continue; //add this line

Let us know if you want us to submit an issue of Github, or if you have taken care of it.

  • Like 1
Link to comment
Share on other sites

Thanks Adrian for looking into it and tracking down the bug. As efficient as always! You've saved me many times before. I will try your hack while Ryan gets a chance to fix this. And yes, I would appreciate it if you could open a bug report on GitHub as I'm certain that you will explain it a lot better than I possibly could.

  • Like 1
Link to comment
Share on other sites

Thanks guys, I have fixed that issue by adding a new getModule($key, $options) method to the Modules class that lets you retrieve a module with $options. In this case an option called 'noPermissionCheck'. The ServicePages module has been updated to use the new method (when available).

Adrian, I wasn't seeing the "it" GET variable. That is a GET variable used internally by PW, but it gets removed before the request starts. So if you are seeing an "it" variable then you probably shouldn't be. At least I'm not seeing it here. I did go ahead and add that as something for ServicePages to ignore though, per your suggestion.

  • Like 1
Link to comment
Share on other sites

Thanks for the fix Ryan - the noPermisionCheck should come in quite handy in other situations too :)

As for the "it" GET variable.

If I go to: 

http://pwtest.dev/service-pages/?name=about

with the following for debugging:

print_r($k);echo $_GET['it'];
foreach($k as $name) {
//if($name=="it") continue;

I get:

Array
(
[0] => it
)
<br />
<b>Notice</b>: Undefined index: it in <b>/Users/ajones/Sites/pwtest/site/modules/ServicePages/ServicePages.module</b> on line <b>175</b><br />
Array
(
[0] => name
)
<br />
<b>Notice</b>: Undefined index: it in <b>/Users/ajones/Sites/pwtest/site/modules/ServicePages/ServicePages.module</b> on line <b>175</b><br />

{"errors":["Field 'it' is not allowed in queries"]}

so $_GET['it'] doesn't exist, but it is part of the $k array.

Link to comment
Share on other sites

  • 4 weeks later...

I need to generate a selector like this:

template=user, roles=faculty, roles=music

Where the result is users with the "faculty" role AND the "music" role.

Is that possible?

I tried:

?template=user&roles=faculty&roles=music

but only the last roles query seems to be included in the selector.

Link to comment
Share on other sites

We are currently using Service Pages to pull data and serve it on our front end. We can also remove null values by iterating to each fields and prepare new set of data with the formatted data we need. All is fine as of now, but it seems that a performance issue could occured if all available fields must return before formatting it. Is there any option to remove null fields via settings?


Link to comment
Share on other sites

  • 1 month later...

Hi guys, hadn't tried this before, seems to be working great, just a couple of questions:

My query works as it should, but there's bunch of fields that include letters like Å, Ä and Ö and these letters are encoded, a title like "Riihimäki" is encoded to "Riihim\u00c5boki", how can I avoid this and just have the values as is?

Further on, the path value (a relative path) is added by default, any chance of overriding or adding to it, so I could get the absolute/full path for each page?

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
×
×
  • Create New...