这篇文章主要介绍了关于封装的php curl请求函数,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
开启调试模式后


使用方法:
优雅花语水彩艺术边框矢量模板适用于设计贺卡、邀请函、证书、海报、书籍和杂志封面、包装设计、服装印花、壁纸、菜单、公告、信息图表、剪贴画以及各种印刷和数字媒体的视觉装饰等相关设计的AI格式素材。
0
立即学习“PHP免费学习笔记(深入)”;
$params = [
'name'=>'name'
];
$url = '';
httpRequest($url,'POST',$params,[],true);/**
* CURL请求
* @param $url 请求url地址
* @param $method 请求方法 get post
* @param null $postfields post数据数组
* @param array $headers 请求header信息
* @param bool|false $debug 调试开启 默认false
* @return mixed
*/
function httpRequest($url, $method = "GET", $postfields = null, $headers = array(), $debug = false)
{
$method = strtoupper($method);
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);//版本
curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");//在HTTP请求中包含一个"User-Agent: "头的字符串。
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
switch ($method) {
case "POST":
curl_setopt($ci, CURLOPT_POST, true);//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
if (!empty($postfields)) {
$tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
}
break;
default:
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
break;
}
$ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
curl_setopt($ci, CURLOPT_URL, $url);//需要获取的URL地址,也可以在curl_init()函数中设置
if ($ssl) {
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
}
//curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
/*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
$response = curl_exec($ci);
$requestinfo = curl_getinfo($ci);
if ($debug) {
$html = <<<HTML
<style>
*{
margin:0;
padding:0;
font-family: "微软雅黑 Light";
color:#777;
}
body{
padding:10px;
}
h1{
border:1px solid #CCCCCC;
height: 40px;
line-height: 40px;
font-size: 20px;
border-radius: 5px;
text-align: center;
background: #F5F5F5;
}
p.box{
padding:10px 0;
}
p.line{
height: 30px;
line-height: 30px;
border-bottom: 1px dashed #ccc;
overflow: auto;
}
p.line:hover{
background: #EEEEEE;;
}
span.title{
display: inline-block;
width:300px;
height: 100%;
}
span.key{
color:red;
display: inline-block;
width:200px;
height: 100%;
}
</style>
<h1>PostData</h1>
<p class="box">
HTML;
$i = 1;
foreach ($postfields as $key => $value) {
$html .= " <p class=\"line\"><span class='key'>{$i}</span><span class=\"title\">{$key}:</span> <span>{$value}</span></p>";
$i++;
}
$html .= <<<HTML
</p>
<h1>Info</h1>
<p class="box">
HTML;
$i = 1;
foreach ($requestinfo as $kye => $value) {
if (is_array($value)) continue;
$html .= " <p class=\"line\"><span class='key'>{$i}</span><span class=\"title\">{$kye}:</span> <span>{$value}</span></p>";
$i++;
}
$html .= <<<HTML
</p>
<h1>Response</h1>
<p class="box">
{$response}
</p>
<p>
HTML;
$html .= <<<HTML
</p>
HTML;
echo $html;
}
curl_close($ci);
return $response;
}相关推荐:
以上就是封装的php cUrl请求函数的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号