Domanda

Ho provato a passare da una precedente richiesta Post a una richiesta Get. Il che presuppone che sia un Get ma alla fine fa un post.

Ho provato quanto segue in PHP:

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, null);
curl_setopt($curl_handle, CURLOPT_POST, FALSE);
curl_setopt($curl_handle, CURLOPT_HTTPGET, TRUE);

Cosa mi sto perdendo?

Ulteriori informazioni: Ho già una connessione impostata per eseguire una richiesta POST. Ciò si completa correttamente, ma in seguito quando provo a riutilizzare la connessione e torno a GET usando i setopts sopra, finisce per fare un POST internamente con intestazioni POST incomplete. Il problema è che crede che stia facendo un GET ma finisce per mettere un'intestazione POST senza il parametro content-length e la connessione fallisce con ERRORE 411.

È stato utile?

Soluzione 3

Risolto: il problema si trova qui:

Ho impostato POST tramite _CUSTOMREQUEST e _POST e il _CUSTOMREQUEST persistito come POST mentre _POST è passato a _HTTPGET . Il server ha assunto l'intestazione da _CUSTOMREQUEST come quella giusta ed è tornato con un 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');

Altri suggerimenti

Assicurati di inserire la stringa di query alla fine dell'URL quando esegui una richiesta 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 dai curl_setopt () docs per CURLOPT_HTTPGET (enfasi aggiunta):

  

[Imposta CURLOPT_HTTPGET uguale a] TRUE su ripristina il metodo di richiesta HTTP su GET.
  Poiché GET è l'impostazione predefinita, questo è necessario solo se il metodo di richiesta è stato modificato.

Aggiungilo prima di chiamare curl_exec ($ curl_handle)

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'GET');

La richiesta CURL per impostazione predefinita è GET, non è necessario impostare alcuna opzione per effettuare una richiesta GET CURL.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top