0

I had start using Postman to send a POST request to a Codeigniter 3 "Api server" (they only do a print_r of $GLOBALS variable) but when I call it, it doesn't show the body data. Well, then I try to use a small script using CURL:

$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL            => "http://localhost/internalportal/Api/autoLogin",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING       => "",
  CURLOPT_MAXREDIRS      => 10,
  CURLOPT_TIMEOUT        => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST  => "POST",
  CURLOPT_POSTFIELDS     => json_encode(array('user' => 'testUser', 'password' => '123')),
  CURLOPT_HTTPHEADER     => array(
    "Authorization: Bearer 2B003E46-674F-48A9-ADD5-101D25322E21",
    "Content-Type: application/json",
    "Accept: application/json"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo "<pre>$response</pre>";

This show me the same response:

[_GET] => Array()
[_POST] => Array ()
[_SERVER] => Array (
    [REDIRECT_STATUS] => 200
    [CONTENT_TYPE] => application/json
    [HTTP_ACCEPT] => */*
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_CONNECTION] => keep-alive
    [CONTENT_LENGTH] => 52
    [SERVER_SOFTWARE] => Apache/2.4.18 (Ubuntu)
    [REQUEST_SCHEME] => http
    [SCRIPT_FILENAME] => /localhost/internalportal/index.php
    [REMOTE_PORT] => 55366
    [REDIRECT_URL] => /localhost/internalportal/Api/autoLogin
    [REDIRECT_QUERY_STRING] => /Api/autoLogin
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => POST
    [QUERY_STRING] =>
    [REQUEST_URI] => /localhost/internalportal/Api/autoLogin
    [SCRIPT_NAME] => /localhost/internalportal/index.php
    [PHP_SELF] => /localhost/internalportal/index.php
)

And no "POST" data is shown. I really don't know what I'm doing wrong.

Derekthar
  • 533
  • 2
  • 12
  • 23

1 Answers1

0

As CBroe says, application/json don't populate the $_POST. To get posted data I must use:

$fp = fopen('php://input', 'r');
Derekthar
  • 533
  • 2
  • 12
  • 23