I am unable to send a parameter to a GET method of an API using the PHP Curl

Asked

Viewed 820 times

1

Hello, I am trying to use Curl to consume an API where there is a method that receives a string as parameter. This method in the API is configured as GET, so it will receive this parameter validate it within the function and return me other information I need.

Take a look at the code:

public function ValidateStudents() {

    $url = "http://apialuno.apis.com/equipes/ValidarAlunos";

    $ch = curl_init();

    $ras = array(
        'ra' => '2013124007,1645948456,2016292895,2017193789'
    );

    curl_setopt($ch, CURLOPT_URL,            $url );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     $ras); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json')); 

    $result = curl_exec($ch);
}

The parameter I commented on is this ras that is in an array. It returns empty. In Postman I managed to use normally, I called the API and in body I put like this:

{
"ra":"2013124007,1645948456,2016292895,2017193789"
}

And when I send by Postman he returns this json:

[
{
    "ra": 2013124007,
    "nome": "WENDER LUI CAMPOS DA SILVA"
},
{
    "ra": 1645948456,
    "nome": "Não encontrado"
},
{
    "ra": 2016292895,
    "nome": "Aluno já pertence a outra equipe"
},
{
    "ra": 2017193789,
    "nome": "Aluno já pertence a outra equipe"
}
] 

I need to have this same return that I had in Postman in the php variable in order to work with the data later. If anyone knows how to fix this I’ll be very grateful!

  • You want to know how to consume this api within Curl, as far as I know it’s just that: $variavel_que_recebe = json_decode($result); you can use file_get_contents() : see here at documentary. also , need not be curl.

  • Thus passes the ras ['ra' => ['2013124007,1645948456,2016292895,2017193789']]

  • I haven’t been able to yet, n I’m able to send the body that are the ras by parameter. By file_get_contents tbm it didn’t work.

  • From what I understood, such doing a search using the POST method sending the parameters as body and with that you have the return of JSON. Your JSON must send as a body when consuming the address data. This address should help https://forum.imasters.com.br/topic/563902-enviar-json-no-body-com-curl/

  • How I can pass JSON on body, can write an example code?

1 answer

0

The problem is that your request is not a valid JSON. You are trying to use JSON, as set in:

curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json')); 

Then you should use the json_encode when sending the request, since you have not done this before:

curl_setopt($ch, CURLOPT_POSTFIELDS,     json_encode($ras)); 

Anyway, this would work:

public function ValidateStudents() {

    $url = "http://apialuno.apis.com/equipes/ValidarAlunos";

    $ch = curl_init();

    $ras = array(
        'ra' => '2013124007,1645948456,2016292895,2017193789'
    );

    curl_setopt($ch, CURLOPT_URL,            $url );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt($ch, CURLOPT_POST,           1 );
    curl_setopt($ch, CURLOPT_POSTFIELDS,     json_encode($ras)); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: application/json')); 

    $result = curl_exec($ch);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.