Consume API with PHP

0

I have api address of a software which I need to create a PHP form and fetch this data.

URL Ex: " link "

With the URL, the following example is provided:

Object: Product

GET /api/produto/listar

Parameters:

? product = 1 & color = 16 & format = json

I've never consumed an API with PHP.

Can anyone give me a hint, example, indicate a link, story, something so that I can start consuming this api?

I tried the following, but it does not return all the fields:

in var_dump it returns me like this:

string (502) "5"

My code looks like this:

<?php

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_URL, "http://ip:porta/api/lista_departamento?cod_departamento=001&$format=json");
	$result = curl_exec($ch);

	curl_close($ch);

	$result = json_decode($result, true);

	echo $result;

?>

When I pass the url in the browser it returns me like this:

{"odata.metadata":"http:\/\/ip:porta\/api\/$metadata#site_join.SITE_JOIN_DEPARTAMENTOS_LISTA","odata.count":1,"value":[{"departamento":20,"cod_departamento":"001","descricao":"MASCULINO"}]}

    
asked by anonymous 06.12.2018 / 11:01

1 answer

0

As Anderson mentioned, you may be making a request HTTP with the library cUrl of PHP.

Considering the information passed, the code would look like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "http://ip-servidor:porta/api/produto/listar?produto=1&cor=16&format=json");
$result = curl_exec($ch);

curl_close($ch);

$result = json_decode($result, true);

As a request via GET, you pass the parameters after the same link, in the ?variavel=valor&variavel=valor structure.

    
06.12.2018 / 11:44