How do I pass parameters by curl?

1

Colleagues,

I'm rephrasing my question. I have a client that I need to integrate with another site by his. I'm using the curl for this. But instead of using the access of the other site, the integration would make the user to log in to our site, would have access to the content of his site, without having to log in again. I am using the following command:

 // Inicia o cURL
$ch = curl_init();
// Define a URL original (do formulário de login)
$campos = array("login"=>$jmUsuario->email,"classroom_id"=>$jmUsuario->email,"sign"=>$sign);
$parametros = json_encode($campos);
curl_setopt($ch, CURLOPT_URL, 'http://www.siteX.com.br/api/auth/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $parametros);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,0);
curl_setopt($ch, CURLOPT_POST, 0);
$content = curl_exec($ch);
//$response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $content;
    
asked by anonymous 17.04.2016 / 15:31

1 answer

3

Step 1

In this section

$campos = array("login"=>$jmUsuario->email,"classroom_id"=>$jmUsuario->email,"sign"=>$sign);
$parametros = json_encode($campos);

Remove this line

$parametros = json_encode($campos);

Step 2

In line
curl_setopt($ch, CURLOPT_POSTFIELDS, $parametros);

Switch by curl_setopt($ch, CURLOPT_POSTFIELDS, $campos);

Explanation:

CURLOPT_POSTFIELDS expects an array or a string in url encoded format.

Note:

This may not be the solution to the problem because it is not clear in the question where the problem is. Nevertheless, it is a suggestion to fix a visible error.

Another problem in the code you posted is in this section

curl_setopt($ch, CURLOPT_POST, 0);

I assume the data should be sent by the POST method because of the CURLOPT_POSTFIELDS parameter, so I suggest you modify it to

curl_setopt($ch, CURLOPT_POST, true);

If you need to follow a redirect, set the following parameters:

curl_setopt($ch, CURLOPT_TIMEOUT, 5); // tempo em segundos
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // em segundos
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // true: permite redirecionamento
curl_setopt($ch, CURLOPT_MAXREDIRS, 1); // quantidade limite de redirecionamentos permitidos

For more options, refer to the list of parameters in the documentation: link

    
17.04.2016 / 20:26