使用curl和php发送POST数据

时间:2009-07-07 10:45:32

标签: php linux apache curl

电贺。

所以,我在Amazon EC2上运行Fedora Core 8。我安装了httpd,php5和libcurl,以及其他一些东西。似乎工作得很好,但后来我意识到我的PHP脚本中没有curl发送POST数据。在命令行中的相同请求工作。我也在我的本地机器(Win XP)和另一台远程机器(Ubuntu)上运行相同的php脚本,它们运行正常,POST数据正在发送,但不在FC8上。它需要任何特殊配置吗?任何防火墙问题?

这是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命令:

curl -d "something=somethingelse" http://foller.me/tmp/postdump.php

我还在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

没有发送POST数据,请参阅?有什么想法吗?

先谢谢大家。 ~K。

3 个答案:

答案 0 :(得分:8)

看起来好像这会将请求从POST转为GET:

curl_setopt($ch, CURLOPT_NOBODY, 0);

删除该行并且它可以正常工作。

  

<强> CURLOPT_NOBODY

     

非零参数告诉库不包含   输出中的身体部分。这只是相关的   具有单独标题和正文部分的协议。

答案 1 :(得分:1)

不是这个领域的专家,但我有自己的工作代码,其工作方式略有不同。也许这会有所帮助

// Open the cURL session
    $curlSession = curl_init();

    // Set the URL
    curl_setopt ($curlSession, CURLOPT_URL, $url);

首先是curl_init()然后设置url,然后再设置...

$rawresponse = curl_exec($curlSession);

,但我不知道,但也许设置网址后会有所作为......?

答案 2 :(得分:0)

还看到了this post,它建议将帖子字段作为数组而不是字符串

发送