apache 使用 curl 和 php 发送 POST 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1091623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending POST data with curl and php
提问by kovshenin
Greets.
问候。
So, I'm running Fedora Core 8 on an Amazon EC2. I installed httpd, php5 and libcurl, and a bunch of other stuff. Seemed to be working great, but then I realized that POST data isn't being sent by curl in my php scripts. Same request in the command line works tho. I also ran the same php scripts on my local machine (Win XP) and another remote machine (Ubuntu), and they run fine, the POST data is being sent, but not on the FC8. Does it require any special configuration? Any firewall issues?
所以,我在 Amazon EC2 上运行 Fedora Core 8。我安装了 httpd、php5 和 libcurl,以及其他一些东西。似乎工作得很好,但后来我意识到我的 php 脚本中的 curl 没有发送 POST 数据。命令行中的相同请求有效。我还在我的本地机器 (Win XP) 和另一台远程机器 (Ubuntu) 上运行了相同的 php 脚本,它们运行良好,正在发送 POST 数据,但不在 FC8 上。需要什么特殊配置吗?有没有防火墙问题?
Here's the PHP code:
这是PHP代码:
error_reporting(E_ALL);
$ch = curl_init("http://foller.me/tmp/postdump.php");
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "something=somewhere");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
Here's the corresponding curl command:
这是相应的 curl 命令:
curl -d "something=somethingelse" http://foller.me/tmp/postdump.php
I also found the corresponding entry in the apache error_log, and here's what I came up with:
我还在 apache 的 error_log 中找到了相应的条目,这是我想出的:
* About to connect() to foller.me port 80 (#0)
* Trying 75.101.138.148... * connected
* Connected to foller.me (75.101.138.148) port 80 (#0)
> GET /tmp/postdump.php HTTP/1.1
Host: foller.me
Accept: */*
< HTTP/1.1 200 OK
< Date: Tue, 07 Jul 2009 10:32:18 GMT
< Server: Apache/2.2.9 (Fedora)
< X-Powered-By: PHP/5.2.6
< Content-Length: 31
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
* Closing connection #0
The POST data isn't being sent, see? Any ideas?
POST 数据没有被发送,看到了吗?有任何想法吗?
Thanks in advance everyone. ~ K.
提前谢谢大家。~K。
回答by BlaM
Looks as if this turns the request from POST to GET:
看起来好像这将请求从 POST 变为 GET:
curl_setopt($ch, CURLOPT_NOBODY, 0);
Remove that line and it works.
删除该行,它的工作原理。
CURLOPT_NOBODY
A non-zero parameter tells the library to not include the body-part in the output. This is only relevant for protocols that have separate header and body parts.
CURLOPT_NOBODY
非零参数告诉库不要在输出中包含正文部分。这仅与具有单独标头和正文部分的协议相关。
回答by Dave Archer
Not an expert in this field but I've got my own working code which works slightly differently. Maybe this will help
不是该领域的专家,但我有自己的工作代码,其工作方式略有不同。也许这会有所帮助
// Open the cURL session
$curlSession = curl_init();
// Set the URL
curl_setopt ($curlSession, CURLOPT_URL, $url);
It does the curl_init() first then sets the url, then later...
它首先执行 curl_init() 然后设置 url,然后...
$rawresponse = curl_exec($curlSession);
i.e I have no idea but perhaps setting the url after makes a difference somehow...?
即我不知道,但也许在以某种方式有所不同之后设置 url ......?

