Suggested techniques for debugging and performing REST calls

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Roots
Registered User
Posts: 39
Joined: Thu Jun 17, 2004 12:53 pm
Location: Austin TX
Contact:

Suggested techniques for debugging and performing REST calls

Post by Roots » Sat Oct 20, 2018 4:39 am

Hello, I am writing a new extension (targetting phpBB 3.2.x) and despite my best efforts, I've been unable to find some information that I would think would be rather simple to find. I'm new to the PHP language and phpBB development, so please forgive my ignorance. I've read through all of the extension documentation and whatever else I could find, but if you know of any docs that answer my questions, please do link me to it. Thanks!

How do I print debug statements?
I have no front-facing pages other than an ACP configuration page. My extension is primarily a number of event listeners that process event data and make POST calls to a REST endpoint to another service. I've been confused on how I can do basic print statements to ensure things like determining that the right functions are being called.

I tried doing a bunch of echo statements from my listener file (echo 'called notify_post_created()';), but that resulted in actually rendering a page in my browser. I'd rather this output go to some standard output or log file instead, especially because there are multiple functions chained together that I want to make sure are being called and if they all tried to render their own page I'd only see the last one.

How do I safely dump or otherwise examine data structures?
I wanted to see what was contained in the $event structures being passed into my functions in my listener class, so I attempted to echo that data:
echo '<pre>';
echo var_dump($event);
echo '</pre>';


I thought doing this would be harmless, but instead it made my browser unresponsive and forced me to kill it and apache. I saw this data being dumped out onto a page (again, I just want to read this output in my terminal or a log file and not rendered as a page) but there was an enormous amount of information that seemed to be continually written and I couldn't even scroll down to the end. It was almost like it was in an infinite loop when dumping this data.

Is there a recommended technique for examining the contents of data structures?

How do I write data to the logs, and what are the best practices for logging?
I know that there's a log table and also an errors_log file, but I'm unaware of how to write to either one. (are there more logs out there?). I definitely need to log errors when my POST calls fail, but I'm uncertain where those logs should be written or how I can find them. Is there some information on best logging practices?

Is there a native way to do POST and other REST calls in phpBB?
My code currently uses the php curl library to perform the POST calls, but I'd rather not require that users install a 3rd party library on their systems that phpBB does not require (because to my knowledge, phpBB does not list this as a requirement). Is there anything native in phpBB that would allow me to make a POST call?

User avatar
Toxyy
Registered User
Posts: 542
Joined: Mon Oct 24, 2016 3:22 pm
Contact:

Re: Suggested techniques for debugging and performing REST calls

Post by Toxyy » Sat Oct 20, 2018 3:37 pm

Roots wrote:
Sat Oct 20, 2018 4:39 am
echo '<pre>';
echo var_dump($event);
echo '</pre>';


I thought doing this would be harmless, but instead it made my browser unresponsive and forced me to kill it and apache.
This works for me, the only time I have the error you describe here is when I try to var dump something way too large, like the $event variable or even sometimes $event['data'] depending on the page. You're going to have to go deeper into that array.
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][RC] Show User Activity

User avatar
GanstaZ
Registered User
Posts: 565
Joined: Wed Oct 11, 2017 10:29 pm
Location: Zverse

Re: Suggested techniques for debugging and performing REST calls

Post by GanstaZ » Sat Oct 20, 2018 3:42 pm

To add => var_dump or print_r doesn't need an echo.
"When answer lies in the question,.. question becomes redundant!"

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 3292
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Suggested techniques for debugging and performing REST calls

Post by kasimi » Sat Oct 20, 2018 9:46 pm

Roots wrote:
Sat Oct 20, 2018 4:39 am
How do I print debug statements?
During development you could use this:

Code: Select all

function dumpy($anything)
{	
    global $phpbb_root_path;
    $log_file = $phpbb_root_path . 'store/my_ext.log';
    $entry = date('Y-m-d H:i:s ') . print_r($anything, true) . PHP_EOL;
    file_put_contents($log_file, $entry, FILE_APPEND | LOCK_EX);
}
Dumping (even complex) data into the log file: dumpy($event);
Roots wrote:
Sat Oct 20, 2018 4:39 am
How do I write data to the logs, and what are the best practices for logging?
phpBB doesn't have a built-in way to log to file. I think the errors_log file you mention is Apache's business.

To write to phpBB's log database table, inject the log service and call the add() method: https://github.com/phpbb/phpbb/blob/rel ... hp#L57-L69 Example call: https://github.com/phpbb/phpbb/blob/rel ... s.php#L195

For logging any kinds of errors I'd recommend using the admin or mod mode, depending on who is supposed to see the log entries.
Roots wrote:
Sat Oct 20, 2018 4:39 am
Is there a native way to do POST and other REST calls in phpBB?
phpBB has a file downloader class but it has rather limited capabilities. Many existing extensions use cURL. Most of the time it's your best bet.

Roots
Registered User
Posts: 39
Joined: Thu Jun 17, 2004 12:53 pm
Location: Austin TX
Contact:

Re: Suggested techniques for debugging and performing REST calls

Post by Roots » Sun Oct 21, 2018 3:22 am

Thanks for the helpful answers everyone. With your help I was able to get the core functionality of my new extension working tonight for the first time. :D

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Toxyy and 8 guests