HorusKol
HorusKol

Reputation: 8696

PHP/cURL - server not receiving POST data

I'm setting up a client/server test scenario on my local machine - I have a test client PHP script which is intended to send XML-RPC to a server PHP script via cURL.

$cnxn = curl_init();

$log = fopen("/var/www/mobile-client.localhost/www/curl.log", "w");

curl_setopt_array($cnxn,
  array(
    CURLOPT_FRESH_CONNECT => false,
    CURLOPT_HEADER => false,
    CURLOPT_HTTPHEADER => array(
      "Content-Type: text/xml",
    ),
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => "m=boomshakalaka",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_STDERR => $log,
    CURLOPT_URL => "http://mobile-server.localhost/rpc/index.php",
    CURLOPT_VERBOSE => true,
  )
);

$response = curl_exec($cnxn);

echo "<pre>";
var_dump(curl_errno($cnxn) . ':' . curl_error($cnxn), "<hr>", $response);

fclose($log);
curl_close($cnxn);

However, when the server responds by returning the contents of the POST array (using var_dump($_POST)), the response is empty.

The contents of the log file I created at /var/www/mobile-client.localhost/www/curl.log has the following content:

* About to connect() to mobile-server.localhost port 80 (#0)
*   Trying 127.0.0.1... * connected
* Connected to mobile-server.localhost (127.0.0.1) port 80 (#0)
> POST /rpc/index.php HTTP/1.1
Host: mobile-server.localhost
Accept: */*
Content-Type: text/xml
Content-Length: 15

< HTTP/1.1 200 OK
< Date: Tue, 25 Jan 2011 04:57:15 GMT
< Server: Apache/2.2.14 (Ubuntu)
< X-Powered-By: PHP/5.3.2-1ubuntu4.7
< Vary: Accept-Encoding
< Content-Length: 1337
< Content-Type: text/html
< 
* Connection #0 to host mobile-server.localhost left intact
* Closing connection #0

I have also monitored my loopback address with Wireshark, and see the following in the capture log:

POST /rpc/index.php HTTP/1.1
Host: mobile-server.localhost
Accept: */*
Content-Type: text/xml
Content-Length: 15

m=boomshakalaka

Where is my POST data disappearing to?

For those interested in the actual server response - the contents of index.php are:

<?php

var_dump(":)");
var_dump("POST: ", $_POST);
var_dump("GET: ", $_GET);
var_dump("SERVER: ", $_SERVER);

Upvotes: 3

Views: 8247

Answers (3)

kaiser
kaiser

Reputation: 22323

If you want to dump the contents of cURL, go with

var_dump( curl_getinfo( $cnxn, CURLINFO_HEADER_OUT ) );

Upvotes: 0

Salman Arshad
Salman Arshad

Reputation: 272006

Commenting out this part should do the trick:

/*
CURLOPT_HTTPHEADER => array(
    "Content-Type: text/xml",
),
*/

Edit ----

You are not required to explicitly specify the content type. cURL will set it automatically depending on whether CURLOPT_POSTFIELDS is a string (application/x-www-form-urlencoded) or an array (multipart/form-data). Reference.

Upvotes: 9

HorusKol
HorusKol

Reputation: 8696

Content-Type had to be specifically set to application/x-www-form-urlencoded (not text/xml)

Upvotes: 0

Related Questions