Make inquiry Httpclient Cakephp

1

I am making a specific query to a value, I have a group of articles and I want to see only one article.

Seeing the Cakephp manual shows me this but I do not know how to use it.

$response = $http->post('http://example.com/api', http_build_query([
    'search' => $this->request->getData('search'),
]));
    
asked by Jhon Bernal 29.06.2018 в 17:15
source

1 answer

0

I'll leave you a brief explanation: First of all you should instantiate the HttpClient class something like this:

use Cake\Http\Client; 
$http = new Client();

Now we must know what method we are going to consume:

  • GET
  • POST
  • PUT
  • DELETE .. ETC ..

GET EXAMPLE (bring a query):

$response = $http->get('http://ejemplo.com/search', ['id' => '1001']);

In this brief example I consult an article / product by id, it can be id, name, code, etc ... it depends on how the service you are going to consume was established. POST EXAMPLE (add / insert data):

$response = $http->post('http://ejemplo.com/articulos/add', [
  'codigo' => 'art-505',
  'nombre' => 'Iphone 6 16gb'
]);

In this case it is the POST method, which I send as parameters code and name to the service in response I have the result of my method. either error or satisfactory.

and so .. you should try with the desired methods and see what results it brings.

I hope it will guide you!

    
answered by 29.06.2018 / 17:42
source