views:

720

answers:

4

I am doing an HTTP POST using cURL

$url = "http://localhost:8080/~demo/cgi-bin/execute/100";

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

//execute post
$result = curl_exec($ch);
echo("$result");
//close connection
curl_close($ch);

The post gets executed, but the response is shown with the error:

The requested URL /~demo/100 was not found on this server.


The above URL, obviously, does not exist not the server because (somehow) cURL has changed the URL.

It should have been /~demo/cgi-bin/execute/100 . This URL works in browser.

Please tell me why does it do that? AND how can i stop this, for what I want?

A: 

It may not be cURL that is changing the URL, rather that the web server is sending a redirect header to cURL, pointing at a different location. Perhaps the following would help:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
David Grant
Just tried! :( It did not work.
Mohit Nanda
Have you tried to visit the URL in your browser?
David Grant
Yes. http://localhost:8080/~demo/cgi-bin/execute/100 works in a browser. But this method doesn't requests it at al.
Mohit Nanda
+2  A: 
  • Install Fiddler.
  • Enable debugging.
  • Visit the site in the browser.
  • Execute php cURL code.

Fiddler will tell you exactly what the web server is receiving and sending. since you are running locally, you can see exactly what php is sending as well. Compare the two and that will tell you the problem.

Byron Whitlock
+1  A: 

Maybe cURL tries to access default http port 80? Try to use

curl_setopt($ch, CURLOPT_PORT, 8080)
Sergii
A: 

where is?

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
SM