PHP curl简单封装 get post

本文阅读 1 分钟
首页 PHP笔记 正文
  1. <?php
  2. /**
  3. * Class Curl curl简单封装 get post
  4. */
  5. class Curl
  6. {
  7. /**
  8. * @brief get请求
  9. * @param $url 请求的url
  10. * @param array $param 请求的参数
  11. * @param array $header 头部数据
  12. * @param int $timeout 超时时间
  13. * @param int $followAction 是否允许被抓取的链接跳转
  14. * @param int $gzip 是否启用gzip压缩
  15. * @param string $format 格式
  16. * @param int $log 是否启用日志
  17. * @return mixed
  18. */
  19. public static function get($url, $param = array(), $header = array(), $timeout = 3, $followAction = 0, $gzip = 0, $format = 'html',$log=0)
  20. {
  21. $ch = curl_init();
  22. if (is_array($param)) {
  23. $url = $url . '?' . http_build_query($param);
  24. }
  25. curl_setopt($ch, CURLOPT_URL, $url);
  26. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  27. curl_setopt($ch, CURLOPT_HEADER, 0);
  28. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  29. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
  30. if ($followAction) {
  31. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //允许被抓取的链接跳转
  32. }
  33. if ($gzip) {
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));
  35. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  36. }
  37. //curl_setopt($ch, CURLOPT_REFERER, '');
  38. $data = curl_exec($ch);
  39. if ($format == 'json') {
  40. $data = json_decode($data, true);
  41. }
  42. if($log){
  43. if($format=='html'){
  44. self::_logCurlInfo($ch,$param,'');
  45. }else{
  46. self::_logCurlInfo($ch,$param,$data);
  47. }
  48. }
  49. curl_close($ch);
  50. return $data;
  51. }
  52. /**
  53. * @brief post请求
  54. * @param $url 请求的url地址
  55. * @param array $param 请求的参数
  56. * @param array $header http头
  57. * @param int $ssl 是否启用ssl
  58. * @param string $format 返回的格式
  59. * @param int $log 是否启用日志
  60. * @return mixed
  61. */
  62. public static function post($url, $param = array(), $header = array(), $ssl = 0, $format = 'json',$log=0)
  63. {
  64. $ch = curl_init();
  65. if (is_array($param)) {
  66. $urlparam = http_build_query($param);
  67. } else if (is_string($param)) { //json字符串
  68. $urlparam = $param;
  69. }
  70. curl_setopt($ch, CURLOPT_URL, $url);
  71. curl_setopt($ch, CURLOPT_TIMEOUT, 120); //设置超时时间
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生的(Raw)输出
  73. curl_setopt($ch, CURLOPT_POST, 1); //POST
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam); //post数据
  75. if ($header) {
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  77. }
  78. if ($ssl) {
  79. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
  80. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); //将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  81. }
  82. $data = curl_exec($ch);
  83. if ($format == 'json') {
  84. $data = json_decode($data, true);
  85. }
  86. if($log){
  87. if($format=='html'){
  88. self::_logCurlInfo($ch,$param,'');
  89. }else{
  90. self::_logCurlInfo($ch,$param,$data);
  91. }
  92. }
  93. curl_close($ch);
  94. return $data;
  95. }
  96. /**
  97. * 请求信息记录日志
  98. * @param $ch curl句柄
  99. * @param $request 请求参数
  100. * @param $response 响应结果
  101. */
  102. private static function _logCurlInfo($ch,$request,$response)
  103. {
  104. $info = curl_getinfo($ch);
  105. $resultFormat = "耗时:[%s] 返回状态:[%s] 请求的url[%s] 请求参数:[%s] 响应结果:[%s] 大小:[%s]kb 速度:[%s]kb/s";
  106. $resultLogMsg = sprintf($resultFormat,$info['total_time'],$info['http_code'],$info['url'],var_export($request,true),var_export($response,true),$info['size_download']/1024,$info['speed_download']/1024);
  107. error_log($resultLogMsg.PHP_EOL,3,self::CURL_LOG_PATH);
  108. }
  109. }
  110. /* example:
  111. echo Curl::get('http://www.baidu.com');
  112. $arr = Curl::post('127.0.0.1/test/test.php',['a'=>1,'b'=>2],'',0);
  113. var_dump($arr);
  114. */
解压密码: detechn或detechn.com

免责声明

本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。

本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。

本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。

PHP判断一个点是否在 一个图形内[射线法]
« 上一篇 07-30
PHP 使用 ping 命令ping ip
下一篇 » 07-30

发表评论