简体   繁体   English

使用curl和php发送POST数据

[英]Sending POST data with curl and php

Greets. 映入眼帘。

So, I'm running Fedora Core 8 on an Amazon EC2. 所以,我在Amazon EC2上运行Fedora Core 8。 I installed httpd, php5 and libcurl, and a bunch of other stuff. 我安装了httpd,php5和libcurl,以及其他一些东西。 Seemed to be working great, but then I realized that POST data isn't being sent by curl in my php scripts. 似乎工作得很好,但后来我意识到我的PHP脚本中没有curl发送POST数据。 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. 我也在我的本地机器(Win XP)和另一台远程机器(Ubuntu)上运行相同的php脚本,它们运行正常,POST数据正在发送,但不在FC8上。 Does it require any special configuration? 它需要任何特殊配置吗? Any firewall issues? 任何防火墙问题?

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? POST数据没有发送,请参阅? Any ideas? 有任何想法吗?

Thanks in advance everyone. 在此先感谢大家。 ~ K. ~K

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 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. 这仅适用于具有单独标题和正文部分的协议。

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);

ie I have no idea but perhaps setting the url after makes a difference somehow...? 即我不知道,但也许设置网址之后有所作为...?

还看到了这篇帖子 ,它建议将帖子字段作为数组而不是字符串发送

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM