Home > Backend Development > PHP Tutorial > A simple way to wrap curl in PHP

A simple way to wrap curl in PHP

小云云
Release: 2023-03-22 15:22:01
Original
3094 people have browsed it

This article mainly shares with you a simple method of encapsulating curl in PHP. It mainly shares with you a small piece of code. I hope it can help you.

/*   
*    
*/function request_curl($url,$data='') {        // 创建一个新cURL资源
        $ch = curl_init();        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        if (strlen($data) > 0) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }        // 抓取URL并把它传递给浏览器
        $html = curl_exec($ch);        // 关闭cURL资源,并且释放系统资源
        curl_close($ch);        return $html;
}
Copy after login

Call usage method

##

$url = "http://baidu.com";$data = 'name=xiaoming&token=123456';echo request_curl($url, $data);
Copy after login
Related recommendations:

PHP encapsulated CURL extension class instance

The above is the detailed content of A simple way to wrap curl in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template