Home > Backend Development > PHP Tutorial > How to Parse JSON Data from a cURL Request and Insert it into a Database?

How to Parse JSON Data from a cURL Request and Insert it into a Database?

Barbara Streisand
Release: 2024-11-21 08:27:12
Original
582 people have browsed it

How to Parse JSON Data from a cURL Request and Insert it into a Database?

JSON Parsing and Database Insertion

Question: How can I parse and utilize JSON data received from a cURL request for database insertion?

Problem Context: Using the example provided in the question, a PHP script is using cURL to send a request and receive a JSON response. The goal is to parse this JSON response and insert the data into a database.

Solution using json_decode:

To parse the JSON data, we will employ the json_decode function. By passing the JSON response as an argument, we can convert it into a PHP object or array. In this case, we choose to interpret it as an array by setting the second argument to true.

Here's an example demonstrating the parsing process:

$result = '{"Cancelled":false,"MessageID":"402f481b-c420-481f-b129-7b2d8ce7cf0a","Queued":false,"SMSError":2,"SMSIncomingMessages":null,"Sent":false,"SentDateTime":"\/Date(-62135578800000-0500)\/"}';
$json = json_decode($result, true);
Copy after login

The result of the json_decode function is assigned to the $json variable, which now holds an array representation of the JSON data. We can now access and manipulate the data as needed.

To insert the data into a database, you can use database-specific functions or libraries to establish a connection, prepare a query, and execute it. The SQL query itself will depend on the structure of your database and the fields you want to insert. Here's a general example of how it might look:

$mysqli = new mysqli("hostname", "username", "password", "database_name");

$sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('" . $json['Cancelled'] . "', '" . $json['MessageID'] . "', '" . $json['SMSError'] . "')";

if ($mysqli->query($sql) === TRUE) {
    echo "Data successfully inserted into the database.";
} else {
    echo "Error inserting data into the database: " . $mysqli->error;
}
Copy after login

By leveraging the json_decode function and appropriate database functions, you can effectively receive, parse, and insert JSON data into your database from your cURL request.

The above is the detailed content of How to Parse JSON Data from a cURL Request and Insert it into a Database?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template