Home >Backend Development >PHP Tutorial >A simple way to wrap curl in PHP
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!