We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Using controllers as task

Hello, I've got a webservice that runs as a module of my phalcon application. All the controller responses are in json and it doesn't use the view. I'd like to call the webservice through the command line but i can't make tasks working with controller actions withour rewriting al the task actions. I want to call an url like https://www.mysite.com/webservice/order/get/323 with:
$ php cli.php webservice order get 323
My cli support the modules. Any ideas?



12.1k

Maybe I've been not clear, sorry. I already have my cli environment. What I need is something like an adapter in order to use my web service controllers through command line without rewriting every single action in tasks (why doing the same thing two times) . the only idea I have is to call directly the web service by curl or wget... But it's very inelegant and slower.



85.5k

you can use exec/shall_exec etc.., call_user_func somehow, no idea really



79.0k
Accepted
answer
edited Oct '17

Well, your API response is JSON, but via HTTP protocol. Thus, you need a way to send HTTP request and fetch response, i.e. trim out HTPP headers. Cli by default does not work with HTTP. So exactly CURL is the way to. You could also go with plain and simple file_get_contents of your endpoints, but I suggest (php) cURL for maximum control over your requests.

That would be microservice architecture, you're using standardized protocol (HTTP) to fetch remote results from another app (in this case your app), connecting two separate apps to work together in one eco system.



12.1k

My API use default module/controller/action/params url. So I can achieve what you are suggesting modifying my Bootstrap and sending every cli command to the main task of webservice module, than using that task for connecting via curl to the webservice controllers. I would have prefered to do everything avoiding external calls but it means I will user localhost address and curl in this way