Having problems getting remote::request to work

Hi folks,

I am trying to make an api call everytime a page of the template “project” changes.
Everything is working so far, despite the Api call, which takes too long and propably ends in a timeout. Calling the same API via Postman works flawless. Do you guys have any Idea on what i am missing?

Thanks in advance,
Tamara

kirby()->hook('panel.page.update', function($page) {

  if ($page->template() != 'project' || $page->foo() == null || $page->foo() == '') {return;}

  $someData = $page->foo();
  $otherData = $page->bar();
  $somethingElse = $page->fooBar();
  $url = 'https://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . 'xyz';
      
  $data = ["someData" => $someData, "otherData" => $otherData, "somethingElse" => $somethingElse];
  $options = [
  'headers' => [
        'Content-Type: application/json'
  ],
  'method' => 'POST',
  'data' => json_encode($data)
  ];
  
  $response = remote::request($url, $options);

});

The URL looks somehow wrong, there’s at least a slash missing after the port.

Hi @texnixe, thanks for your reply. Unfortunately I did a mistake by coping the url. In the original code there’s a slash after the port like. As I said it works perfectly in Postman…

$url = 'https://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . '/xyz';

Then try and save $response to a file somewhere or try to error_log it, so that you get a proper error message. Or try the code in a template first.

Unfortunately there’s no proper error message. Seems like $response is empty. Do you have any Idea on what could cause the empty response?

You might have to convert your field variables into values, rather then using the field objects:

 $someData = $page->foo()->value();
 $otherData = $page->bar()->value();
 $somethingElse = $page->fooBar()->value();

It’s crashing after converting line 45: $somethingElse = $page->fooBar()->value();

Error msg:

Call to a member function value() on string in file: xyz line 45

I’m really lost :slightly_frowning_face:

Is the server from where your hook is fired in any way not able to reach the endpoint of the api url? Could you check your network logs for that? And could you dump the content of $url and $options just before you call remote::request?

What is foobar()? Looks like that is not a field… It helps if you provide real data, not foos and bars… :wink: If a value is already a string (as the error message tells us), you cannot call a field value on it to convert it into a string.

While $page->title() returns an object of type field, $page->id() only returns a string.

In the documentation for each method, you can find the return types, e.g. http://k2.getkirby.com/docs/cheatsheet/page/id

Your field in the content file always return a field object.

Hi @texnixe,
thanks for your support. You’ve been right, I have made the API-Call with one of my variables being a field object, instead of a string. Adding ->value() did the trick. Thanks for your effort!