cURL在POST后更改URL

时间:2021-11-01 19:44:32

I am doing an HTTP POST using cURL

我正在使用cURL进行HTTP POST

$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.

在此服务器上找不到请求的URL / ~demo / 100。


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

显然,上面的URL不存在服务器,因为(某种程度上)cURL已经更改了URL。

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

应该是/ ~demo / cgi-bin / execute / 100。此URL适用于浏览器。

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

请告诉我为什么这样做?我怎么能阻止这个,为了我想要的?

4 个解决方案

#1


  • Install Fiddler.
  • Enable debugging.
  • Visit the site in the browser.
  • 在浏览器中访问该站点。

  • Execute php cURL code.
  • 执行php cURL代码。

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.

Fiddler将告诉您Web服务器正在接收和发送的内容。既然你在本地运行,你也可以看到php发送的确切内容。比较两者,这将告诉你问题。

#2


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

也许cURL尝试访问默认的http端口80?尝试使用

curl_setopt($ch, CURLOPT_PORT, 8080)

#3


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:

它可能不是更改URL的cURL,而是Web服务器正在向cURL发送重定向标头,指向不同的位置。也许以下内容会有所帮助:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

#4


where is?

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

#1


  • Install Fiddler.
  • Enable debugging.
  • Visit the site in the browser.
  • 在浏览器中访问该站点。

  • Execute php cURL code.
  • 执行php cURL代码。

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.

Fiddler将告诉您Web服务器正在接收和发送的内容。既然你在本地运行,你也可以看到php发送的确切内容。比较两者,这将告诉你问题。

#2


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

也许cURL尝试访问默认的http端口80?尝试使用

curl_setopt($ch, CURLOPT_PORT, 8080)

#3


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:

它可能不是更改URL的cURL,而是Web服务器正在向cURL发送重定向标头,指向不同的位置。也许以下内容会有所帮助:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

#4


where is?

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);