如何将PHP的HTTP请求方法封装成高效的长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计260个文字,预计阅读时间需要2分钟。
phpClass CurlCurl类简单封装get和post方法
Curl@brief get请求@param $url 请求的url@param array $param 请求的参数@param array $header 头部数据@param int $timeout 超时时间@param int $follow 跟随重定向
<?php /** * Class Curl curl简单封装 get post */ class Curl { /** * @brief get请求 * @param $url 请求的url * @param array $param 请求的参数 * @param array $header 头部数据 * @param int $timeout 超时时间 * @param int $followAction 是否允许被抓取的链接跳转 * @param int $gzip 是否启用gzip压缩 * @param string $format 格式 * @param int $log 是否启用日志 * @return mixed */ public static function get($url, $param = array(), $header = array(), $timeout = 3, $followAction = 0, $gzip = 0, $format = 'html',$log=0) { $ch = curl_init(); if (is_array($param)) { $url = $url . '?' . www.google.com/bot.html)"); if ($followAction) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //允许被抓取的链接跳转 } if ($gzip) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate')); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); } //curl_setopt($ch, CURLOPT_REFERER, ''); $data = curl_exec($ch); if ($format == 'json') { $data = json_decode($data, true); } if($log){ if($format=='html'){ self::_logCurlInfo($ch,$param,''); }else{ self::_logCurlInfo($ch,$param,$data); } } curl_close($ch); return $data; } /** * @brief post请求 * @param $url 请求的url地址 * @param array $param 请求的参数 * @param array $header www.baidu.com'); $arr = Curl::post('127.0.0.1/test/test.php',['a'=>1,'b'=>2],'',0); var_dump($arr); */
本文共计260个文字,预计阅读时间需要2分钟。
phpClass CurlCurl类简单封装get和post方法
Curl@brief get请求@param $url 请求的url@param array $param 请求的参数@param array $header 头部数据@param int $timeout 超时时间@param int $follow 跟随重定向
<?php /** * Class Curl curl简单封装 get post */ class Curl { /** * @brief get请求 * @param $url 请求的url * @param array $param 请求的参数 * @param array $header 头部数据 * @param int $timeout 超时时间 * @param int $followAction 是否允许被抓取的链接跳转 * @param int $gzip 是否启用gzip压缩 * @param string $format 格式 * @param int $log 是否启用日志 * @return mixed */ public static function get($url, $param = array(), $header = array(), $timeout = 3, $followAction = 0, $gzip = 0, $format = 'html',$log=0) { $ch = curl_init(); if (is_array($param)) { $url = $url . '?' . www.google.com/bot.html)"); if ($followAction) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //允许被抓取的链接跳转 } if ($gzip) { curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate')); curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); } //curl_setopt($ch, CURLOPT_REFERER, ''); $data = curl_exec($ch); if ($format == 'json') { $data = json_decode($data, true); } if($log){ if($format=='html'){ self::_logCurlInfo($ch,$param,''); }else{ self::_logCurlInfo($ch,$param,$data); } } curl_close($ch); return $data; } /** * @brief post请求 * @param $url 请求的url地址 * @param array $param 请求的参数 * @param array $header www.baidu.com'); $arr = Curl::post('127.0.0.1/test/test.php',['a'=>1,'b'=>2],'',0); var_dump($arr); */

