curl实现GET、POST请求

  1 <?php
  2 
  3 /**
  4   * curl 模拟 GET 请求
  5    * @param string $url  请求的URL
  6    * @param bool|true $ssl  是否启用 ssl证书验证
  7    * @param array $headers  设置 HTTP 头字段的数组,格式: array('Content-type: text/plain', 'Content-length: 100')
  8    * @return bool|mixed
  9   *
 10   */    
 11 function _requestGet($url, $ssl=true, $headers=array())
 12 {
 13     # curl完成初始化
 14     $curl = curl_init();
 15 
 16     # curl 选项设置
 17     curl_setopt($curl, CURLOPT_URL, $url); //需要获取的URL地址
 18 
 19     $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36';
 20     curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);   # 在HTTP请求中包含一个"User-Agent: "头的字符串,声明用什么浏览器来打开目标网页
 21 
 22     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);     # TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。
 23 
 24     curl_setopt($curl, CURLOPT_AUTOREFERER, true);        # TRUE 时将根据 Location: 重定向时,自动设置 header 中的Referer:信息。
 25 
 26     curl_setopt($curl, CURLOPT_TIMEOUT, 30);              # 设置超时时间
 27 
 28     curl_setopt($curl, CURLOPT_ENCODING, '');             
 29     # HTTP请求头中"Accept-Encoding: "的值。 这使得能够解码响应的内容。 支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",会发送所有支持的编码类型
 30 
 31     if($headers) {
 32             curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);  # 设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100')
 33     }
 34 
 35     # SSL相关,https需开启
 36     if ($ssl) {
 37         curl_setopt($curl, CURLOPT_CAINFO, '/cert/ca.crt');  # CA 证书地址
 38         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);    # 禁用后cURL将终止从服务端进行验证
 39         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);        
 40         # 设置为 1 是检查服务器SSL证书中是否存在一个公用名;设置成 2,会检查公用名是否存在,并且是否与提供的主机名匹配;0 为不检查名称。 在生产环境中,这个值应该是 2(默认值)。
 41         # 公用名(Common Name)一般来讲就是填写你将要申请SSL证书的域名 (domain)或子域名(sub domain)
 42     }else {
 43         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    # 禁用后cURL将终止从服务端进行验证,默认为 true
 44     }
 45 
 46     curl_setopt($curl, CURLOPT_HEADER, false);             # 是否处理响应头,启用时会将头文件的信息作为数据流输出
 47 
 48     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);      # TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。
 49 
 50     # 执行 curl 会话
 51     $response = curl_exec($curl);
 52 
 53     if (false === $response) {
 54         echo '<br>', curl_error($curl), '<br>';
 55         return false;
 56     }
 57     #关闭 curl会话
 58     curl_close($curl);
 59     return $response;
 60 }
 61 
 62 
 63 /**
 64   * curl 模拟 POST 请求
 65    * @param string $url  请求的URL
 66    * @param array $data  请求数据
 67    * @param bool|true $ssl  是否启用 ssl证书验证
 68    * @param array $headers  设置 HTTP 头字段的数组,格式: array('Content-type: text/plain', 'Content-length: 100')
 69    * @return bool|mixed
 70   *
 71   */    
 72 function _requestPost($url, $data, $ssl=true, $headers=array())
 73 {
 74     # curl完成初始化
 75     $curl = curl_init();
 76 
 77     # curl 选项设置
 78     curl_setopt($curl, CURLOPT_URL, $url); //需要获取的URL地址
 79 
 80     $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36';
 81     curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);   # 在HTTP请求中包含一个"User-Agent: "头的字符串,声明用什么浏览器来打开目标网页
 82 
 83     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);     # TRUE 时将会根据服务器返回 HTTP 头中的 "Location: " 重定向。
 84 
 85     curl_setopt($curl, CURLOPT_AUTOREFERER, true);        # TRUE 时将根据 Location: 重定向时,自动设置 header 中的Referer:信息。
 86 
 87     curl_setopt($curl, CURLOPT_TIMEOUT, 30);              # 设置超时时间
 88 
 89     curl_setopt($curl, CURLOPT_ENCODING, '');             
 90     # HTTP请求头中"Accept-Encoding: "的值。 这使得能够解码响应的内容。 支持的编码有"identity","deflate"和"gzip"。如果为空字符串"",会发送所有支持的编码类型
 91 
 92     if($headers) {
 93             curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);  # 设置 HTTP 头字段的数组。格式: array('Content-type: text/plain', 'Content-length: 100')
 94     }
 95 
 96     # SSL相关,https需开启
 97     if ($ssl) {
 98         curl_setopt($curl, CURLOPT_CAINFO, '/cert/ca.crt');  # CA 证书地址
 99         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);    # 禁用后cURL将终止从服务端进行验证
100         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);        
101         # 设置为 1 是检查服务器SSL证书中是否存在一个公用名;设置成 2,会检查公用名是否存在,并且是否与提供的主机名匹配;0 为不检查名称。 在生产环境中,这个值应该是 2(默认值)。
102         # 公用名(Common Name)一般来讲就是填写你将要申请SSL证书的域名 (domain)或子域名(sub domain)
103     }else {
104         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);    # 禁用后cURL将终止从服务端进行验证,默认为 true
105     }
106 
107 
108     curl_setopt($curl, CURLOPT_POST, true);                # 是否为POST请求
109 
110     curl_setopt($curl, CURLOPT_POSTFIELDS, $data);         # 处理请求数据,全部数据使用HTTP协议中的 "POST" 操作来发送
111 
112 
113     curl_setopt($curl, CURLOPT_HEADER, false);             # 是否处理响应头,启用时会将头文件的信息作为数据流输出
114 
115     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);      # TRUE 将curl_exec()获取的信息以字符串返回,而不是直接输出。
116 
117     # 执行 curl 会话
118     $response = curl_exec($curl);
119 
120     if (false === $response) {
121         echo '<br>', curl_error($curl), '<br>';
122         return false;
123     }
124     #关闭 curl会话
125     curl_close($curl);
126     return $response;
127 }
原文地址:https://www.cnblogs.com/jinxiblog/p/7930129.html