Home >Backend Development >PHP Problem >How to add parameters in php curl post

How to add parameters in php curl post

藏色散人
藏色散人Original
2022-10-18 10:09:542131browse

How to add parameters to php curl post: 1. Open the corresponding PHP code file; 2. Create "public function curl_post($url, $data=array()){...}"; 3. Just add the post variables through "curl_setopt($ch, CURLOPT_POSTFIELDS, $data);".

How to add parameters in php curl post

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

How to php curl post Add parameters?

php curl sends a post request with parameters

public function curl_post($url , $data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        // POST数据
        curl_setopt($ch, CURLOPT_POST, 1);
        // 把post的变量加上
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

Note:

PHP supports the libcurl library created by Daniel Stenberg that allows you to use various servers with various types of protocols for connection and communication.

libcurl currently supports http, https, ftp, gopher, telnet, dict, file and ldap protocols. libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (this can also be completed through PHP's FTP extension), HTTP form-based upload, proxy, cookies and username and password authentication.

How to use cURL to implement Get and Post requests in PHP

These functions were introduced in PHP 4.0.2.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to add parameters in php curl post. 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