I am unable to retrieve the information sent through CURL using the POST method

Recently, I started using Postman to send a POST request to an "Api server" built with Codeigniter 3 (which only prints the $GLOBALS variable). However, when I make the call, the body data doesn't appear. So, I decided to try 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>";

Surprisingly, this displays the same response as before:

[_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
)

Unfortunately, no "POST" data is being shown. It's puzzling why it's not working as expected.

Answer №1

In the words of CBroe, when using application/json, the data is not populated in $_POST. To retrieve posted data, one must utilize the following code:

$fp = fopen('php://input', 'r');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I need help creating a symmetric key of AES-256 in PHP

Despite my extensive search efforts on Google, I have not been able to find a direct and clear answer to my question. Therefore, a simple yes or no response would be greatly appreciated. I have been tasked with creating an "AES-256 symmetric (also known a ...

Transforming the JSON body request from Postman into a Jmeter body request

const today = new Date(); const time = today.getTime(); pm.environment.set('time', time); let eventArray = []; for(let i = 1; i <= 50; i++) { let t = time + (i * 1000); eventArray.push({ "eid": i, "time": t }); } con ...

To spice things up, how about this: "Revamp the <ul> list by breaking it and generating a new one

I am trying to generate multiple ul classes with li content from a foreach loop, but only if the values count exceeds 5. $data = array('Barcelona','Jujuy','Cordoba','Mendoza','Galicia','Madrid',& ...

Having difficulty naming the request while using ajax to send data to a local server

I have set up an AJAX request to be sent to my PHP server: var xml = new XMLHttpRequest(); xml.open("POST", "request.php", true); xml.setRequestHeader('query','test'); xml.send(); Upon receiving the AJAX data in PHP, I am facing some ...

Sending JSON data to a targeted object in iOS

After reading numerous posts on Stack about POSTing data in XML and JSON, I am still having trouble finding specific information on how to update a selected object. I am successfully retrieving data from my boss' job tracking API and everything seems ...

Verify the presence of the promotion code and redirect accordingly

I have created a special promotion page that I want to restrict access to only users who have received a unique code from me via email. To achieve this, I have designed the following form: <form accept-charset="UTF-8" action="promotion.php" method="po ...

Is it required to set a $basepath when incorporating Twig with Slim 3?

Currently, I am utilizing the Twig-View feature of Slim framework from https://github.com/slimphp/Twig-View, to render Twig templates within my application. The detailed instructions provided on the respective pages make the integration process quite stra ...

How to Customize the Length of Password Reset Token in Laravel 5.4?

Working with Laravel 5.4 to create an API, I encountered a problem with the token sent to users for password reset. The current token has 64 characters which are too long for users to easily grab. Is there a way to configure Laravel to generate a custom ...

The count in the document is not increasing

I am facing an issue where a file on the server contains a counter, and when a link is clicked, an ajax request is sent to a PHP file to increment the counter in the file. However, the file is being created without the counter being incremented. Interesti ...

Exploring Node and Express: Uncovering all custom methods using the .all() function

As I work with Node and Express, my goal is to capture all traffic directed towards a specific URL using the following code: APP.all('/testCase', function(req, res) { console.log('I am called with the method: ' + req.method); }); ...

What steps do I need to take in order to integrate an mpg video onto my

I am in need of embedding mpg (dvd compliant mpeg2) movie files onto my webpage. Unfortunately, I do not have the ability to convert these videos into any other format. This webpage is solely for personal use, so any solution would be greatly appreciated. ...

When the json string contains more bytes than visible characters, the json_decode() function may encounter errors

It seems like there's an issue with this code snippet: $jsonDecode = json_decode($jsonData, TRUE); Surprisingly, when I manually place the string from $jsonData inside the decode function, it works perfectly fine. This solution does work: $jsonDecod ...

The Laravel model's attribute appears as empty in the JSON response

In my Laravel 4.2 project, I have a model with a 'status' attribute and declared an accessor to handle it: public function getStatusAttribute($status) { if (isset($this->expires_at) && strtotime(\Carbon\Carbon::now()) > ...

interactive textbox created with the combination of javascript and php

Hello, I am new to JavaScript and jQuery. I am trying to create a dynamic text box using JavaScript that can add and remove rows. When I press the add button, it works well, but when I pressed delete, it deleted the entire table. Below is my JavaScript fu ...

Converting a hexadecimal value to a signed integer in PHP: A step-by-step guide

I need help converting a hexadecimal string into a signed integer value. While I can easily convert it into an unsigned value using hexdec(), I am struggling to get a signed value. Here is the VB code snippet I am working on, with "AA" as the example hex ...

PHP Namespaces: Importing Functions

Exploring functional programming in PHP sounds like an exciting endeavor. It's always refreshing to step out of the realm of OOP in PHP and try something new. When it comes to using namespaces, I prefer to keep them aligned with the file structure: ...

PHP SQL injects null every nine months

Encountering a strange SQL error where 0000-00-00 is being entered into the database on 2018-9-31. I am unsure why only zeros are showing up for that date. Any assistance would be greatly appreciated, as this issue occurs every third quarter insert. INSER ...

Empty space under the banner in Wordpress theme Avada

I can't seem to locate the CSS class for a blank space that appears below the header on one of my pages. This space is situated between the header and "SERVICIOS 24 HORAS". Any ideas on how I can remove this mysterious gap? My website is built wit ...

Retrieve data from an SQL database and populate an HTML dropdown menu in a web page using PHP

I am a beginner in the world of JavaScript and facing some challenges. I am working with PHP 7 and attempting to retrieve data from an SQL database, specifically a table. My goal is to extract a single column from this table and display it in a dropdown me ...

Instantly share your videos on YouTube straight from your Mac computer

I am currently working on an OS X application using XCode and I would like to enable my users to upload videos directly to YouTube from their Mac. Similar to how iMovie functions. Currently, I am uploading the videos to a server and then manually to my ow ...