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

A simple way to wrap curl in PHP

小云云
小云云Original
2018-03-29 09:28:132863browse

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;
}

Call usage method

##

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

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!

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