How do I pass parameters through Curl?

Asked

Viewed 1,342 times

1

Colleagues,

I’m rephrasing my question. I have a client I need to integrate another site with. I’m using Curl for this. But instead of the user using the access of the other site, the integration would make the user when logging into our site, would have access to the content of his site, without needing to login again. I’m 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;
  • "If you are the application Owner, check your logs for Details." = > "If you are the application owner, check your logs for details"

  • There Bacco. The mistake I understood, but I need to know how to correct this error.

  • This is not the error, this is a warning of where you have to look to know the error that is preventing the page from being displayed. And it is just by looking at us logs that you will find the exact reason for the problem. Whenever you are working with error output turned off, it is critical to see the logs (actually, it’s important to see the logs even with error display on).

  • Got it...I fixed it and it appeared now You are being redirected. Another thing, when I wrote "There" in my comment, was to be "Hello" rs rs rs rs rs.

  • Figured out the problem? If so, you can post it as an answer (if you think it’s something that can happen to other users). If it’s just a syntax problem, or a very specific one, it might be best to just remove the doubt. See if it applies more to your problem. Answer the question itself is valid within the rules of the site, and even encouraged when the person resolves on their own.

  • I found nothing... I’m taking a beating with this.. rs rs.. Regarding the time, I put curl_setopt($ch, CURLOPT_TIMEOUT,0); and is directing correctly, but is giving error 404, only that the parameters are correct. I will redo my current code in the post.

Show 1 more comment

1 answer

3


Step 1

In this passage

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

Remove that line

$parametros = json_encode($campos);

Step 2

On the line
curl_setopt($ch, CURLOPT_POSTFIELDS, $parametros);

Exchange for
curl_setopt($ch, CURLOPT_POSTFIELDS, $campos);

Explanation:

CURLOPT_POSTFIELDS expects an array or string in url format encoded.

Obs:

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 correct a visible error.

Another problem in the code you posted is in this excerpt

curl_setopt($ch, CURLOPT_POST, 0);

I assume the data should be sent by the method POST due to parameter CURLOPT_POSTFIELDS, so I suggest you change 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, see the list of parameters in the documentation: http://php.net/manual/en/function.curl-setopt.php

  • Where you put $field I think you wanted to put $fields.

  • Hello Daniel. I did as you said, but now appears "You are being redirected." but does not redirect.

  • In this case you should define other parameters. CURLOPT_MAXREDIRS, CURLOPT_FOLLOWLOCATION, CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT

  • edited with examples..

  • Perfect Daniel. It worked, but just one more question. These commands have the same effect as a POST form, because the client’s proposal is to make an integration in the system, where the user when accessing the client’s system, can view the integrated external site without the need to log in again. The parameters I passed are the same as a form. The codes would have the same functionality as if the user had done by the form?

  • 1

    Exactly. It’s like a <form action.... method=post> <input type=submit>... . One more piece of advice, as you are dealing with authentication, involves a user’s password. Think of a way to work with authentication tokens to ensure greater security and privacy for your users. Think of a way where not even you who is the programmer of the site, can not know the password of users. Example: http://answall.com/a/123543/4793

  • Thanks Daniel for the help and clarification.

Show 2 more comments

Browser other questions tagged

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