Jump to content

Anyone created a PW get api?


benbyf
 Share

Recommended Posts

HI!

I'm finding myself more and more balancing making get and post requests to templates or to admin modules, when really it would be nice to send all requests to a API url and get data back or a receipt.

My example would be to click a link in an email to then make a setting change in PW, like change a specific user role, or publish a page draft etc.

Has anyone done much of this and how did you structure it?

I was thinking of maybe creating a template for my API and then using the $input inputs and whether you are logged in or not as starting points for sending data back for my API requests specific to my website (a news site with hundreds of members).

Link to comment
Share on other sites

May be you can use two approaches.

One is creating an API inside PW using special templates and responses.

maybe using my Rest Helper :)

https://github.com/NinjasCL/pw-rest
heres an example https://github.com/NinjasCL/voxgram

The other one is creating an API outside PW using an specialized rest framework
and calling PW using composer.

http://flightphp.com
http://phpsx.org
http://phpflow.com/php/restful-api-frameworks-for-php/

  • Like 3
Link to comment
Share on other sites

Technically my rest helper is just some functions that I used when creating simple Rest backend with ProcessWire.
So is nothing too fancy, just what I needed. See the voxgram repo that used the Rest Helper :) .

The magic with PW is that you can integrate it with more sophisticated tools like
Symphony components using composer if you want to.

May be I should write a simple tutorial using Rest and PW?
 

  • Like 8
Link to comment
Share on other sites

For testing purposes I am running a simple website providing vat exchange rates related to german tax law via api in different formats.
http://umsatzsteuer-umrechnungskurse.org

I have a template api.php where the api get vars are sanitized. A custom module process the input and provide the requested data by sending headers.

One output function of my module as an example:

    /**
     * @param int/string year
     * @param int/string to year (optional)
     * @param bool return the € value referenced to the listed currency
     * @return bool/ json document
     *
     */
    public function outputJSON($from_year, $to_year = null, $invert = false) {
        if (false === $json = $this->getStringJSON($from_year, $to_year, $invert)) return false;
        header('Content-Type: application/json; charset=utf-8');
        print $json;
    }   


my template api.php

$config->prependTemplateFile and $config->appendTemplateFile disabled via module settings.

<?php
// a data request
if ($input->get->k) {

    // get the module
    $gvr = $modules->get('GermanVatExchangeRate');

    // sanitize input
    $key = $sanitizer->name($input->get->k);
    $checkKey = $gvr->keyCheck($key, $max);

   // ...
   try {
            $result = $gvr->convert($curr, $value, $month, $year, $inv);
            // log api access
            $gvr->log($key);
        }
        catch (WireException $e) {
            $result = "ERROR: ".$e->getMessage();
        }
        die($result);
    }
   // ...

   // something went wrong
   die('ERROR: Not specified');
}

// not a data request render api page
else {
    include_once('_init.php');
    $content = $page->body;
    include_once('_main.php');
}

 

  • Like 2
Link to comment
Share on other sites

14 hours ago, clsource said:

The magic with PW is that you can integrate it with more sophisticated tools like
Symphony components using composer if you want to.

May be I should write a simple tutorial using Rest and PW?
 

+1 tutorial for simple api and using PW with composer.

@kixe think I've bee working on a similar structure with a special api.php template that disables preappend and append files.

  • Like 1
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

×
×
  • Create New...