Http(s) API call with custom local domain (valet)

hi!

i’m trying to use Remote::get() and fetch the kirby apis of the current website i’m working on.

i use valet to run my local php env. valet uses dnsmasq. i can access the custom domain perfectly either through the browser as well as pinging it form the terminal (which returns 127.0.0.1).

when i do the Remote::get() call i get a No Message response. if i use a full url (eg my custom valet url) I get a dns problem saying it could not resolve the domain. if i use http://127.0.0.1 i also get a No message exception (kirby/src/Http/Remote.php 228).

this my plugin

<?php

Kirby::plugin('trust/api', [
  'routes' => [
    [
      'pattern' => 'tapi',
      'method' => 'GET',
      'action' => function () {
        $response = Remote::get('/api/pages/home?select=id,title,url,drafts,content', [
          'headers' => [
            'Authorization: Basic ' . base64_encode($email . ':' . $password)
          ]
        ]);

        return $response;
      }
    ]
  ]
]);

?>

$email and $password are added manually for now but i’ll query them through a $kirby callback.

config.php settings

'api' => [
    'basicAuth' => true
  ]

NB I am able to fetch the kirby apis from javascript, but i am in a situation where for some views of the app i am doing 4-5 calls and it slows down a lot the rendering. therefore i was thinking of setting routes for each view and pre-loading the json endpoint with the necessary data for the js fetch call upfront.

any hint appreciated! i might be mis-configuring the kirby plugin maybe.

I had the same issue with valet and the custom domain. It works if I call the URL from a REST app like RESTED.

Can you curl it manually from terminal? If that works, you can possibly use fetch() instead?

yes! doing it with curl works fine!

i’ll try fetch() and will report back, thanks.


@jimbobrjames could you make an example with fetch() inside a plugin?

this is my first attempt, but docs are lacking examples. can fetch() take an array of arguments like new Remote() does? can i do ['u', 'username:password'] like in curl?

$response = $remote->fetch('/api/pages/home?select=id,title,url,drafts,content', ['u', 'email:password']);

return $response;

this call return an error

undefined variable remote

if i do

$kirby = kirby();
$response = $kirby->remote->fetch('/api/pages/home?select=id,title,url,drafts,content', ['u', 'email:password']);

i get undefined property : Kirby\Cms\App::$remote.

I’ve never tried it im afraid, and im not the best with this kind of thing, but from poking around, i think you need to go through new:Remote which runs it through fetch.

See here https://getkirby.com/docs/reference/@/http/remote/__construct

Something like (you might have to give it a full url)…

$response = new Remote('/api/pages/home?select=id,title,url,drafts,content', ['u', 'email:password']);

return $response;

yes i tried the new Remote() at first, and also now once more but i get

Could not resolve: trust.test (Domain name not found)

ouch.

with curl i do

curl -u email:password -k https://trust.test/api/site/children?select=id,title,url,content

-k tells curl not to check the https certificate,

and i get the desired results.

Does it work if you turn off Valet SSL and do it just with http? Im thinking the request via the plugin knows the certificate isnt “real”. However the default seems to be false i think for checking it, so I’m out of ideas.

thanks again. using an http version did not help, same error. i guess i’ll move on the website to my vps and work from there, developing the js app locally.

Ok i think im on to something, i believe you need your local machine to be first thing the machine tries for DNS lookups before moving on to remote DNS servers… remember to keep whatever is already set for DNS, just make sure localhost is at the top.

See here, might be other help thats useful in this thread…

@texnixe Maybe this fixes the issue you were having too?

i tried both the 127.0.0.1 setting as well as uninstalling openssl to match the same curl version with php but no luck :smiley:

i’m a bit confused now as i began using valet rather the php -S because of kirby 3 new architecture. i have no desire to run mamp. i guess it’s an edge case for now so i’ll simply upload the kirby server on my vps and see if that resolves this problem.

thanks for you help @jimbobrjames!

Hmm strange, I was sure that would solve it. You did reboot afterwards i take it, or flush the DNS from terminal?

I use Valet myself and i have a feeling i’m going to run into this as well on my next project. If I solve it I will let you know.

yep i rebooted machine as well as valet and brew.

to be honest, after i added the 127.0.0.1 the first time, the http call went well! then i updated the rest of the code to get the data in the frontend and i realized my internet went away, so i added a second dns entry after 127… and from there on it did not work. i tried to take out all dns entries and put back only 127…, i reverted back my code etc.

not sure why that first time worked, unless i hallucinated lol.

Weird. I like Valet because it’s very lightweight (Personally I use Valet+ which is an unofficial fork that adds a few features).

If i need a bit more power or something more real I use Homestead or Scotchbox which are both Vagrant based.

Try edition your /etc/hosts file, mapping 127.0.0.1 to your custom domain. Worked in my test.

Thanks! I tried, but no luck.

Just for anyone else who wants to test modifying the /etc/hosts file:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

# Adding this last line for an example domain 315.test solved it for me
127.0.0.1 325.test

(Haven’t tested with HTTPS, though)

btw, any chance the Kirby APIs will get extended to be able to fetch multiple pages in one http call?

eg

/api/pages={page1, page2, page3..}

?

May I suggest you create an issue in the ideas repo if you haven’t already?

Today I tested the solution found by @jimbobrjames and putting 127.0.0.1 first as DNS server works in my environment as well.

Interesting :slight_smile: Glad that worked.