/** * curl请求-支持 get post put delete * @param $url * @param $type * @param array $params * @param array $headers * @return bool|string */ function commonRequest($url, $type, $params = [], $headers = []) { $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); switch ($type) { case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true); break; case "POST": curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); break; case "PUT" : curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); break; case "DELETE": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); break; } $file_contents = curl_exec($ch);//获得返回值 curl_close($ch); return $file_contents; }
php curl方法 封装 支持get post put 等
最新推荐文章于 2024-05-23 16:09:21 发布