powershellUsing the cURL command with a PHP script

I am having trouble sending JSON data from the command line with curl to a php script and properly receiving it on the server-side.

Here is what I have attempted so far:

Running the following command in the command line:

curl -H "Content-Type: application/json" -X POST -i -d '{"token":"xyz"}' http://localhost/api/users.php

And here's the users.php file:

// Receive the RAW post data.
$content = file_get_contents("php://input");
echo $content;

// Try to decode JSON
$decoded = json_decode($content);

if ($decoded) { 
    echo "success";
    echo $decoded['token'];
} else {
    echo "failed";
}

The result that I get is:

'{token:xyz}'
failed

I have also tried escaping the double quotes ("") in the curl command like this:

curl -H "Content-Type: application/json" -X POST -i -d '{\"token\":\"xyz\"}' http://localhost/api/users.php

However, I still receive almost the same result:

'{"token":"xyz"}'
failed

My issue seems to be that the php script fails to correctly convert the received JSON data.

What could be causing this problem?

EDIT (Solved):

Try running the curl command with double quotes and escape quotes in between:

curl -H "Content-Type: application/json" -X POST -i -d "{\"token\":\"xyz\"}" http://localhost/api/users.php

Answer №1

It appears to be fine; however, upon testing your provided example, I've noticed that it only functions correctly when double quotes are placed around the post data. Give it a try.

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

Retrieve information from a date that has yet to occur

I am facing a challenge in MYSQL where I need to display all data that hasn't been updated in the past 13 days. Despite my efforts, I have encountered some issues. Here is the SQL query I attempted: "SELECT * FROM TABLE WHERE datediff( curdate( ) , ...

Exploring the Past: How the History API, Ajax Pages, and

I have a layout for my website that looks like this IMAGE I am experimenting with creating page transitions using ajax and the history API. CODE: history.pushState(null, null, "/members/" + dataLink + ".php" ); // update URL console. ...

Retrieve the hour details from a given date string

Can we retrieve the hour value from a date string as shown below? 20151211091805 +0000 Desired outcome- 09:18:05 (this should also be in string format) ...

How does cPanel go about setting up user accounts?

Is there a way to programmatically create websites/accounts on a server without using cPanel, such as through node.js or PHP? How does cPanel handle this process? I understand that websites are typically generated by making changes to the Apache configura ...

What is the method for sending an AJAX request with a dynamically looping ID number parameter in the URL

I am looking to make multiple AJAX calls with a loop parameter named [id] in the URL, starting from request.php?id=1 and ending at id=9. I want to send each call after a 3-second delay. As a JavaScript beginner, I'm unsure of where to begin implementi ...

Tips for updating the value of a JSON by accessing the specific path in Java

I am trying to change the value of "$.widget.debug" in my code. I have experimented with numerous libraries, but haven't been successful in achieving this task. { "widget": { "debug": "on", "window": { "title": "Sample ...

What is the best way to identify modifications in a JSON object?

import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.0 Window { visible: true width: 640 height: 480 property var json: { "name": "old" } Component.onCompleted: json.name = "new" onJsonChanged: co ...

The image fails to load when attempting to retrieve it from a local JSON file

I successfully managed to fetch data dynamically from a local JSON file created in RN. However, when I tried to add images for each profile to be displayed along with the dynamic profile info, the app encountered an error stating that "The component cannot ...

Calculate the product of a JavaScript variable and a PHP variable, then show the result on the

I need to calculate the product of a PHP variable value and a JavaScript variable, then show the result on the screen. Here is my PHP code: <?php require 'config.php'; session_start(); ?> <html> <head> < ...

Tips for including URL in AJAX calls

Hey, I've been trying to use AJAX to pass my PHP file URL and a textbox value but it's not working. Can someone help me figure out what's wrong in my code? Here is the snippet: function displayRecords(numRecords, pageNum ) { $.ajax({ ...

An issue occurred while making an HTTP PUT request to Shopify due to a problem with sending an array in the JSON request for

Here is a description of how to update the metafield of an existing article in Shopify using an http PUT response. For detailed API documentation on Articles, click here func UploadArticleListImg(client *http.Client, ArticleIDs []string, imageURL []string ...

Laravel 5.8 does not support the POST method for this route. The supported methods are GET and HEAD

I am facing an issue while saving my data to the user table in Laravel using Ajax. I have created an API that runs successfully with a POST method giving a success message. However, when I attempt to save data using Ajax, I encounter the following error: ...

I need the links to open up the content area on the website in HTML

Is it possible to have a fixed header that does not disappear when clicking links from the navigation bar? I tried dividing the page into tables to achieve this. Now, I want to open the links in the navigation bar in the right column (the main content col ...

Using Java's JsonPath library to insert an object into another object

Currently utilizing the JayWay JsonPath library for JSON object manipulation. In need of inserting a JSON object into an existing JSON array: ### before { "students": [] } ### after { "students": [{"id": 1, "name&qu ...

Using JSON to bind values to an array of checkboxes

I am working with an array of checkboxes that are populated from a collection. Here is the code snippet: <div class="form-group"> Select days in a week : <td class="even" *ngFor="let item of dayList"> <input value="{{item.check ...

Dynamic Intro based on day of the week - PHP

Hello everyone, I am seeking assistance as a beginner in php and I have encountered a problem that I haven't been able to solve. The objective is to have a unique Video-Intro for each day of the week, with different versions of the video available fo ...

Creating a dynamic display of flash files on a webpage through a unique combination of PHP, AJAX, and

Despite my lack of experience with Ajax and minimal java knowledge, I have a strong background in SQL and PHP. Even though I anticipate criticism for this question, I am determined to seek help. My primary objective is to rotate 4 flash SWF files randomly ...

Trouble with AJAX communicating with PHP and not rendering fresh data

I have created a row of images that are clickable, and once clicked, a variable is sent via AJAX to a PHP file for a database query. The PHP file receives the variable and executes the correct query. However, instead of updating the HTML on my page, it sim ...

The jQuery AJAX delete function is only able to capture the initial form submission

I am facing an issue with making an AJAX call to a PHP file to delete a row in the database. The problem is that it only deletes the first row. Despite my extensive research on this matter, I have not been able to find a solution. Therefore, I am reaching ...

Inject information into an HTML element

Currently, I am fetching data from MySQL and the task at hand is to display the result in a specific class. To achieve this, I have employed jQuery to update every 10 seconds successfully. However, the challenge lies in transferring that data into the desi ...