How can I get a web service with Phalcon?



2.0k
edited 25d ago

According to the Phalcon Docs, you can request doing the following:

<?php

use Phalcon\Http\Request;

// Getting a request instance
$request = new Request();

// Check whether the request was made with method POST
if ($request->isPost()) {
    // Check whether the request was made with Ajax
    if ($request->isAjax()) {
        echo 'Request was made using POST and AJAX';
    }
}

Then, you can send a response (see the Phalcon Docs):

<?php

use Phalcon\Http\Response;

// Getting a response instance
$response = new Response();

// Set status code
$response->setStatusCode(404, 'Not Found');

// Set the content of the response
$response->setContent("Sorry, the page doesn't exist");

// Send response to the client
$response->send();

You can json_encode your content to parse it via AJAX.

Do I already have the service, as a consumer, the service from a Phalcon controller?



2.0k

You need to put the code in the controller, in the specific action function.

With the normal PHP functions I get an error:

Fatal error: Uncaught Error: Access to undeclared static property: Phalcon\Di::$_default in C:\xampp7\htdocs\restfull-copia\public\index.php:17Stack trace:#0 [internal function]: Phalcon\Di->construct()#1 C:\xampp7\htdocs\restfull-copia\public\index.php(17): Phalcon\Di\FactoryDefault->construct()#2 {main}Next Error: Access to undeclared static property: Phalcon\Di::$_default in C:\xampp7\htdocs\restfull-copia\public\index.php:17Stack trace:#0 [internal function]: Phalcon\Di->construct()#1 C:\xampp7\htdocs\restfull-copia\public\index.php(17): Phalcon\Di\FactoryDefault->construct()#2 {main} thrown in C:\xampp7\htdocs\restfull-copia\public\index.php on line 17

with what functions could I bring the URL information?



2.0k
edited 25d ago

it depends of the url structure!

if you want to bring information form http://localhost/restfull-copia/users/

Then you need to put the code in the IndexAction function of UsersController class, but if the url is like http://localhost/restfull-copia/users/search/13

Then you if you need to get 13 in the searchAction of your UsersController class,

public function searchAction($id) {
  echo $id;
  // You can instead return a response following the code in my first answer.
}
edited 25d ago

What I try to do is:

$page = file_get_contents('http://localhost/restfull-copia/api/robots'); echo $page

that's a function to consume with PHP,But in Phalcon I get the error

my question is, with what function do I access from my project to the web service that is in that url?

This is a known bug on windows, you need to use php-cgi or vagrant or something like this.

I hope you only use that code for quick testing?

What I try to do is:

$page = file_get_contents('http://localhost/restfull-copia/api/robots'); echo $page

that's a function to consume with PHP,But in Phalcon I get the error

my question is, with what function do I access from my project to the web service that is in that url?