使用curl和php发送POST数据
Greets。
所以,我在Amazon EC2上运行Fedora Core 8。我安装了httpd,php5和libcurl,还有一堆其他东西。似乎工作很好,但后来我意识到POST数据不是由我的PHP脚本中的curl发送。在命令行中的相同的请求工作tho。我也在我的本地机器(Win XP)和另一个远程机器(Ubuntu)上运行相同的php脚本,他们运行良好,POST数据正在发送,但不是在FC8。它需要任何特殊的配置吗?任何防火墙问题?
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?
这里是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);
这是相应的curl命令:
Here's the corresponding curl command:
curl -d "something=somethingelse" http://foller.me/tmp/postdump.php
我还在apache error_log中找到了相应的条目,这里是我想到的:
I also found the corresponding entry in the apache error_log, and here's what I came up with:
* 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
POST数据未发送,任何想法?
The POST data isn't being sent, see? Any ideas?
先感谢大家。
〜K。
Thanks in advance everyone. ~ K.
看起来像是将请求从POST转到GET:
Looks as if this turns the request from POST to GET:
curl_setopt($ch, CURLOPT_NOBODY, 0);
删除该行,它可以正常工作。
Remove that line and it works.
CURLOPT_NOBODY
非零参数告诉库不包括
body-part在输出。这仅与具有单独的标题和主体部分的
协议相关。
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.