Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending POST data with curl and php

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?

Here's the PHP code:

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 -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:

* 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?

Thanks in advance everyone. ~ K.

like image 873
kovshenin Avatar asked Jan 23 '23 11:01

kovshenin


2 Answers

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

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.

like image 174
BlaM Avatar answered Jan 31 '23 15:01

BlaM


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...

$rawresponse = curl_exec($curlSession);

i.e I have no idea but perhaps setting the url after makes a difference somehow...?

like image 32
Dave Archer Avatar answered Jan 31 '23 15:01

Dave Archer