[GH-ISSUE #942] Using PHP example from doc for publishing JSON notification sends JSON object and is not interpreted by the server #660

Closed
opened 2026-05-07 00:26:21 +02:00 by BreizhHardware · 4 comments

Originally created by @godsgood33 on GitHub (Nov 9, 2023).
Original GitHub issue: https://github.com/binwiederhier/ntfy/issues/942

🐞 Describe the bug
I am converting my notifier from PushBullet to a self-hosted Ntfy instance. It work great and the messages come through just like you think they should. However, the notification received is just the JSON object and not an interpreted display. Maybe I am doing something wrong.

💻 Components impacted
all

💡 Screenshots and/or logs
Code I wrote to send the message using the example in the doc:

$auth = base64_encode($_SERVER['NTFY_USERNAME'].':'.$_SERVER['NTFY_PASSWORD']);
file_get_contents(
      filename: "{$_SERVER['NTFY_BASE_URL']}/{$message->topic}",
      context: stream_context_create([
           'http' => [
               'method' => 'POST',
               'header' =>
                   "Content-Type: application/json\r\n" .
                   "Title: {$message->title}\r\n" .
                   "Authorization: Basic {$auth}",
               'content' => json_encode($message)
           ]
      ])
);

This is what the notification looks from the web app
image

🔮 Additional context
I tried looking at the symfony/ntfy-notifier, but I am currently using Symfony 5.4 and that requires at least 6.4 and symfony/notifier 6.2

Originally created by @godsgood33 on GitHub (Nov 9, 2023). Original GitHub issue: https://github.com/binwiederhier/ntfy/issues/942 :lady_beetle: **Describe the bug** I am converting my notifier from PushBullet to a self-hosted Ntfy instance. It work great and the messages come through just like you think they should. However, the notification received is just the JSON object and not an interpreted display. Maybe I am doing something wrong. :computer: **Components impacted** all :bulb: **Screenshots and/or logs** Code I wrote to send the message using the example in the doc: ```php $auth = base64_encode($_SERVER['NTFY_USERNAME'].':'.$_SERVER['NTFY_PASSWORD']); file_get_contents( filename: "{$_SERVER['NTFY_BASE_URL']}/{$message->topic}", context: stream_context_create([ 'http' => [ 'method' => 'POST', 'header' => "Content-Type: application/json\r\n" . "Title: {$message->title}\r\n" . "Authorization: Basic {$auth}", 'content' => json_encode($message) ] ]) ); ``` This is what the notification looks from the web app ![image](https://github.com/binwiederhier/ntfy/assets/1922710/d0b37d26-6ec9-4dc5-893c-42a7576f9252) :crystal_ball: **Additional context** I tried looking at the symfony/ntfy-notifier, but I am currently using Symfony 5.4 and that requires at least 6.4 and symfony/notifier 6.2
BreizhHardware 2026-05-07 00:26:21 +02:00
  • closed this issue
  • added the
    🪲 bug
    label
Author
Owner

@wunter8 commented on GitHub (Nov 9, 2023):

Messages with JSON bodies need to be sent to ntfy.domain.com and not ntfy.domain.com/topic

<!-- gh-comment-id:1804741474 --> @wunter8 commented on GitHub (Nov 9, 2023): Messages with JSON bodies need to be sent to `ntfy.domain.com` and not `ntfy.domain.com/topic`
Author
Owner

@godsgood33 commented on GitHub (Nov 10, 2023):

Messages with JSON bodies need to be sent to ntfy.domain.com and not ntfy.domain.com/topic

That didn't work. I got a 400 error. With and without the trailing '/'.

PHP Warning:  file_get_contents(https://notify.domain.com/): Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
<!-- gh-comment-id:1804864093 --> @godsgood33 commented on GitHub (Nov 10, 2023): > Messages with JSON bodies need to be sent to `ntfy.domain.com` and not `ntfy.domain.com/topic` That didn't work. I got a 400 error. With and without the trailing '/'. ``` PHP Warning: file_get_contents(https://notify.domain.com/): Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request ```
Author
Owner

@wunter8 commented on GitHub (Nov 10, 2023):

I'm guessing your JSON is malformed. Specifically, markdown has to be a bool, not '1', so change your PHP message array to ... ,'message' => true, ...

<!-- gh-comment-id:1804892887 --> @wunter8 commented on GitHub (Nov 10, 2023): I'm guessing your JSON is malformed. Specifically, `markdown` has to be a bool, not '1', so change your PHP message array to `... ,'message' => true, ...`
Author
Owner

@godsgood33 commented on GitHub (Nov 10, 2023):

That seemed to work. Turns out the problem was in the __set method...I accidentally put string for the second parameter which cast the true to a string 1. Thanks!

<!-- gh-comment-id:1804915613 --> @godsgood33 commented on GitHub (Nov 10, 2023): That seemed to work. Turns out the problem was in the `__set` method...I accidentally put `string` for the second parameter which cast the `true` to a string `1`. Thanks!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ntfy#660
No description provided.