1    /**
  2      *
  3      * [curl_post post方式请求]
  4      *
  5      * @param  [type] $url  [description]
  6      *
  7      * @param  string $data [description]
  8      *
  9      * @return [type]       [description]
 10      *
 11      */
 12 
 13 
 14     protected function curl_post($url,$data='')
 15 
 16     {
 17 
 18         if(empty($url) || empty($data)){
 19 
 20             return false;
 21 
 22         }
 23 
 24         $postUrl = $url;
 25 
 26         $curlPost = $data;
 27 
 28         //初始化curl
 29 
 30         $ch = curl_init();
 31 
 32         //抓取指定网页
 33 
 34         curl_setopt($ch, CURLOPT_URL,$postUrl);
 35 
 36         //设置header
 37 
 38         curl_setopt($ch, CURLOPT_HEADER, 0);
 39 
 40         //要求结果为字符串且输出到屏幕上
 41 
 42         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 43 
 44         //post提交方式
 45 
 46         curl_setopt($ch, CURLOPT_POST, 1);
 47 
 48         curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
 49 
 50         //运行curl
 51 
 52         $data = curl_exec($ch);
 53 
 54         curl_close($ch);
 55 
 56         
 57 
 58         return $data;
 59 
 60     }
 61 
 62     /**
 63      *
 64      * [curl_get get方式请求]
 65      *
 66      * @param  [type] $url  [description]
 67      *
 68      * @param  string $data [description]
 69      *
 70      * @return [type]       [description]
 71      *
 72      */
 73 
 74     protected function curl_get($url,$data='')
 75 
 76     {
 77 
 78         if(empty($url)){
 79 
 80             return false;
 81 
 82         }
 83 
 84         //初始化
 85 
 86         $ch = curl_init();
 87 
 88 
 89 
 90         curl_setopt($ch, CURLOPT_URL,$url);
 91 
 92         // 执行后不直接打印出来
 93 
 94         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 95 
 96         curl_setopt($ch, CURLOPT_HEADER, false);
 97 
 98         // 跳过证书检查
 99 
100         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
101 
102         // 不从证书中检查SSL加密算法是否存在
103 
104         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
105 
106 
107 
108         //执行并获取HTML文档内容
109 
110         $output = curl_exec($ch);
111 
112 
113 
114         //释放curl句柄
115 
116         curl_close($ch);
117 
118         $output = json_decode($output,1);
119 
120         return $output;
121 
122     }

 

posted on 2019-01-07 23:41  Cc_Pz  阅读(576)  评论(0编辑  收藏  举报