Cómo cambiar de POST a GET en PHP CURL
He intentado cambiar de una solicitud Post anterior a una solicitud Get. Que asume que es un Get pero finalmente hace un post.
Probé lo siguiente en PHP:
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null);
curl_setopt($curl_handle, CURLOPT_POST, FALSE);
curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);
¿Qué me estoy perdiendo?
Información Adicional: Ya tengo una conexión que está configurada para hacer una solicitud POST. Eso se completa con éxito, pero más tarde, cuando intento reutilizar la conexión y volver a cambiar para OBTENER el uso de las opciones anteriores, todavía termina haciendo un POST internamente con POST incompleto cabecera. El problema es que cree que está haciendo un GET, pero termina poniendo un encabezado POST sin el parámetro content-length y la conexión falla con un ERROR 411.
4 answers
Asegúrese de poner su cadena de consulta al final de su URL cuando realice una solicitud GET.
$qry_str = "?x=10&y=20"; $ch = curl_init(); // Set query data here with the URL curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php' . $qry_str); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); $content = trim(curl_exec($ch)); curl_close($ch); print $content;
With a POST you pass the data via the CURLOPT_POSTFIELDS option instead of passing it in the CURLOPT__URL. ------------------------------------------------------------------------- $qry_str = "x=10&y=20"; curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 3); // Set request method to POST curl_setopt($ch, CURLOPT_POST, 1); // Set query data here with CURLOPT_POSTFIELDS curl_setopt($ch, CURLOPT_POSTFIELDS, $qry_str); $content = trim(curl_exec($ch)); curl_close($ch); print $content;
Nota de la curl_setopt()
docs para CURLOPT_HTTPGET
(énfasis añadido):
[Set CURLOPT_HTTPGET equal to]
TRUE
to reset the HTTP request method to GET.
Dado que GET es el valor predeterminado, esto solo es necesario si se ha cambiado el método de solicitud.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-18 12:13:09
Agregue esto antes de llamar a curl_exec (cur curl_handle)
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-04 06:20:54
Resuelto: El problema está aquí:
I conjunto POST
por tanto _CUSTOMREQUEST
y _POST
y el _CUSTOMREQUEST
persistió como POST
while _POST
cambiado a _HTTPGET
. El Servidor asumió que el encabezado de _CUSTOMREQUEST
era el correcto y regresó con un 411.
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-10-24 12:12:55
CURL request de forma predeterminada es GET, no tiene que establecer ninguna opción para realizar una solicitud GET CURL.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-17 00:36:47