1

I have created a file on bluehost server test.php and used this file send curl request from another server ( godaddy ).

$url = 'http://dev.testserver.com/test.php';
        $data_string = json_encode($fields);
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, 1); 
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($curl, CURLOPT_POSTFIELDS,$data_string );                                                                  
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                                                                      
        $curl_response = curl_exec($curl);

How to capture posted data on test.php and process it ? I am trying with $_POST but its showing blank.

Vikram
  • 3,171
  • 7
  • 37
  • 67
  • 2
    Possible duplicate of [How to get body of a POST in php?](http://stackoverflow.com/questions/8945879/how-to-get-body-of-a-post-in-php) – Victor Smirnov May 04 '16 at 06:40
  • @VictorSmirnov i dont see any similarity between these two question. Other question is just for $_POST and I mentioned about specific curl but not the POST. But thanks for the link, answer for that is quite useful. – Vikram May 04 '16 at 07:42
  • I think the question was 'How to capture posted data on test.php and process it?' You send JSON data but not 'application/x-www-form-urlencoded' data, this is why you can not read it with `$_POST`. The other answer was for the question - how to send date using POST to make sure it is available with `$_POST`. May be this is what you need but formally you've asked a different question. – Victor Smirnov May 04 '16 at 08:29

2 Answers2

2

The question is close to this one How to send raw POST data with cURL? (PHP)

I've slightly modified your client code to follow recommendations:

<?php

$fields = ['a' => 'aaaa', 'b' => 'bbbb'];

$url = 'http://localhost/test.php';
$data_string = json_encode($fields);

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, urlencode($data_string));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$curl_response = curl_exec($curl);

echo 'Response: '.$curl_response.PHP_EOL;

I do urlencode for the sent data and set headers.

The logic how to read data is explained in the question How to get body of a POST in php? I made a simple test.php file with the following code

<?php

$body = file_get_contents('php://input');
if (!empty($body)) {
    $data = json_decode(urldecode($body), true);
    var_export($data);
}

We read the data, decode it and parse JSON.

As one might expect the test output from the client script is the following

$ php client.php 
Response: array (
  'a' => 'aaaa',
  'b' => 'bbbb',
)
Community
  • 1
  • 1
Victor Smirnov
  • 3,450
  • 4
  • 30
  • 49
1

Try to replace this, direct send array not json

curl_setopt($curl, CURLOPT_POSTFIELDS,$data_string );

With

curl_setopt($curl, CURLOPT_POSTFIELDS,$fields );

Check this : http://php.net/manual/en/function.curl-setopt.php

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109