标签:manual 发送 border strip agent xxx 方式 请求头 fun
http://php.net/manual/zh/function.curl-setopt.php
<?php /** * 发送CURL请求 * @param $url 请求的URL * @param $method 请求方法 * @param $params 参数(关联数组形式) * @param array $header 一维数组的请求头信息(非关联数组)。 * @return bool */ function curl($url, $method = ‘GET‘, $params = [], $header = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //将获取的信息以字符串返回,而不是直接输出 curl_setopt($ch, CURLOPT_URL, $method == "POST" ? $url : $url . ‘?‘ . http_build_query($params)); //解析参数 //设置超时时间 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($ch, CURLOPT_TIMEOUT, 60); //如果是https协议,取消检测SSL证书 if (stripos($url, "https://") !== FALSE) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //CURL_SSLVERSION_TLSv1 curl_setopt($ch, CURLOPT_SSLVERSION, 1); } //判断是否设置请求头 if (count($header) >= 1) { curl_setopt($ch, CURLOPT_HTTPHEADER, $header); } //通过POST方式提交 if ($method == "POST") { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); } $data = curl_exec($ch); //执行curl操作 curl_close($ch); //关闭curl操作 return $data; } /*$header = [ ‘User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘, ‘Accept-Language:zh-CN,zh,q=0.8‘ ];*/ $url = "http://xxx.com"; $header[] = ‘User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6‘; $header[] = ‘Accept-Language:zh-CN,zh,q=0.8‘; //参数 $vars = [‘a‘ => 1, ‘b‘ => ‘lo‘]; $res = curl($url, ‘GET‘, $vars, $header); var_dump($res);
标签:manual 发送 border strip agent xxx 方式 请求头 fun
原文地址:https://www.cnblogs.com/php99/p/7aec9172c2d5ab69f2523b35bdd2b1af.html