PDA

View Full Version : curl issues



nostalgia
18 Jan 2010, 11:20 AM
Hey all. I'm trying to do a POST with cURL. I've found plenty of sample code online and have been through the documentation, but am still having trouble. First, here's the code:



<?php
echo "<p>START</p>";

$url = "http://myserver.com/temp.php";

$ch = curl_init($url);
echo "<p>[".curl_error($ch)."]</p>";

if (!$ch) {
echo "<p>curl_init() failed</p>";
}
else{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);

$recData = curl_exec($ch);
$info = curl_getinfo($ch);

if ($recData === false || $info['http_code'] != 200)
{
$recData = "Failure: [". $info['http_code']. "]";
if (curl_error($ch))
{
$recData .= "\n". curl_error($ch);
}
echo "<p>$recData</p>";
}
else
{
echo "<p>Success</p>";
}
curl_close($ch);
}
echo "<p>DONE</p>";
?>

And here's what that prints out:



START

[]

Failure: [403]

DONE

Now if I do a regular HTML post it works fine.



<form action="temp.php" method="post">
<input type="submit" value="Press me">
</form>

I've been doing some HTTP sniffing with Wireshark, and it turns out when I attempt to do the cURL post, I see a GET being issued. When I do the HTML form POST, I do see a POST being issued.

I've tried this on two different servers. On the other server, curl_exec() just hangs and times out after 30 seconds.

Any ideas on what I could be doing wrong here are very welcome. I'm at my wit's end.

-Joe