Home  >  Article  >  Backend Development  >  How to implement raw transmission database in php

How to implement raw transmission database in php

藏色散人
藏色散人Original
2022-10-27 10:24:001446browse

php method to implement raw transmission database: 1. Create a PHP sample file; 2. Use CURL to send postman’s raw format data, with code such as “curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')".

How to implement raw transmission database in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, Dell G3 computer.

How does PHP implement raw transmission to the database?

PHP uses CURL to send postman’s raw format data

I have been using What is sent directly is a string in a single format. When connecting to the external site data today, they require an object format string, which is the raw format data in postman.

    $data = "{
       name: 'job',
       age: 20,
       hobby: [
            {
                before: 'Play games',
                now: 'read book'
            }
       ]
    }";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        )
    );
    $response = curl_exec($ch);
    $response = json_decode($response);

It is worth noting that The header must have `'Content-Type: application/json'. Without the type, the receiving end will not be able to parse.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to implement raw transmission database in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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