【问题标题】:Sending POST data with curl and php使用 curl 和 php 发送 POST 数据
【发布时间】:2010-11-08 15:54:45
【问题描述】:

问候。

所以,我在 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。

【问题讨论】:

    标签: php linux apache curl


    【解决方案1】:

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

    curl_setopt($ch, CURLOPT_NOBODY, 0);
    

    删除该行即可。

    CURLOPT_NOBODY

    一个非零参数告诉库不包含 输出中的正文部分。这仅适用于 具有单独的标头和正文部分的协议。

    【讨论】:

    • 我一直在寻找那 4 个小时。哈哈!非常感谢人=)
    【解决方案2】:

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

    // Open the cURL session
        $curlSession = curl_init();
    
        // Set the URL
        curl_setopt ($curlSession, CURLOPT_URL, $url);
    

    它首先执行 curl_init() 然后设置 url,然后...

    $rawresponse = curl_exec($curlSession);
    

    也就是说,我不知道,但也许设置 url 之后会有所不同...?

    【讨论】:

    • 感谢大卫的尝试,但不幸的是它并没有改变任何东西。它确实可以在我的另外两台机器上运行;)但不是我想要的,呵呵
    • 是的,哦!我刚看了说明书,我的回答很垃圾!抱歉,我帮不上忙
    • 那么 curl_setopt ($curlSession, CURLOPT_POST, 1);而不是“真实”。再次从我自己的剪切和粘贴代码中猜测
    • 不 ;) 没关系
    【解决方案3】:

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

    【讨论】:

    • 那我也卡住了,再次抱歉
    • 没有问题的人,我一直在寻找答案大约 4 小时 ;) 感谢您的努力
    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多