Echo or return result in PHP API call?

I am writing an API call, which will be called using cURL in Bash, maybe some other shells, and potentially other PHP scripts in the future. Currently, I am echoing the result to the screen for debugging purposes, but I was wondering if it would be more beneficial for me to return the result instead. What are the upsides and downsides of echoing or returning my result? Thanks!

return… to what?

To the user who queries the API call. Am I mistaken in thinking I could use return to supply my output to the user?

Yes, you are mistaken. return will work in the context of the executing script.

1 Like

Maybe my understanding of what an API is isn’t completely correct, I think of an API as something like a “contract” between the consumer and the provider. eg.

“If you make a request in this way you will get this”

I guess text/html could be returned, but from the few APIs I’ve looked into they return XML or nowadays increasingly more often JSON.

XML is good if you need strict control over the data definition, but JSON is often sufficient.

What happens at the backend is of no concern to the API consumer. It could be Perl, PHP, etc.etc.
What is of concern to the API consumer is being able to make a request and get back what they want.

1 Like

Apologies for my lack of details. This is not a fully-fledged API, but just an API call to be used internally in my organization. But I did review requirements with those who would use it, and I resolved my issue. Thank you all!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.