cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Does libcurl POST to the current location, instead of the set URL?

From: Matthias B. <matthiasb_at_zenbe.com>
Date: Thu, 25 Jun 2009 01:45:28 +0200

Daniel Stenberg schrieb:
> On Wed, 24 Jun 2009, Matthias B. wrote:
>
>> After I created the curl-Resource, I set curlopt_url, curlopt_post,
>> curlopt_followlocation, curlopt_returntransfer, and curlopt_postfields.
>>
>> After the first execution, the script go in a for-loop. In this loop
>> the curlopt_postfields gets changed, and curl gets executed again.
>> But: It doesn't POST to the URL which was set before - it POST to the
>> current Location - so I need to set the URL everytime.
>>
>> Why is that behavior? Is that documented?
>
> You didn't really give us a lot of details here but it sounds like you
> get a 301, 302 or 303 redirect. You can use CURLOPT_POSTREDIR to alter
> how libcurl acts on 301 and 302.
>
I get a 302-redirect. May I make a short pseudo-code sketch:

$ch=curl_init();
CURLOPT_URL=>$posturl
CURLOPT_POST=>1
CURLOPT_POSTFIELDS=>$postfields
CURLOPT_FOLLOWLOCATION=>1
CURLOPT_RETURNTRANSFER=>1
$response = curl_exec($ch);

for ($i=1; $i<=5; $i++) {
CURLOPT_POSTFIELDS=>$postfields.'&a='.$i
$responsearr[] = curl_exec($ch)
}
curl_close($ch);

After the first POST-Execution I get a 302-Redirect. When I now change
the CURLOPT_POSTFIELDS and execute again, libcurl doesn't post to
$posturl (as set before) - libcurl posts to the Location to which it was
redirected. I have to set the CURLOPT_URL after every execution again to
the correct, original URL.

That's not a big problem, but it was something which made me wonder, and
may this behavior should be mentioned in the docs (I couldn't find any
notice to this behavior)?
Received on 2009-06-25