- 09/30草莓互联2024年国庆假期...
- 09/15草莓互联2024年中秋节放...
- 08/10宝塔控制面板手动安装Ope...
- 06/07草莓互联2024年端午节放...
- 04/30草莓互联2024年五一劳动...
- 04/03草莓互联2024年清明放假...
- 03/24宝塔控制面板Nginx后端...
- 02/27宝塔面板python2.7升级...
- 02/19宝塔面板站点PCI DSS不合...
- 02/19宝塔面板站点站长工具中点亮HT...
- 02/14宝塔面板https协议无法获取...
- 02/11宝塔Windows面板下SQL...
- 02/02宝塔控制面板利用nginx实现...
- 09/18宝塔控制面板FTP增加Flas...
phpweb已经是国内最强的的开源电子商务系统了,但并非完美,因为任何...
互联网3年一轮回,京东凡客等电商品牌在三年之前也不过是小打小闹,而如...
电商已经成为趋势,传统行业的竞争已经逐渐演变到网络上,也有不少用Weboss...
phpweb系统是固定的模式,适用于大部分行业与需求。但有一些行业还是有自己...
PHP封装的Curl方法
个人对于Curl还是蛮常用的,偶然发现一个封装这么完全的,记录下。
代码如下:
<?php
function teacher_curl($url, $paras = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (@$paras['Header']) {
$Header = $paras['Header'];
} else {
$Header[] = "Accept:*/*";
$Header[] = "Accept-Encoding:gzip,deflate,sdch";
$Header[] = "Accept-Language:zh-CN,zh;q=0.8";
$Header[] = "Connection:close";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $Header);
if (@$paras['ctime']) { // 连接超时
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $paras['ctime']);
} else {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
}
if (@$paras['rtime']) { // 读取超时
curl_setopt($ch, CURLOPT_TIMEOUT, $paras['rtime']);
}
if (@$paras['post']) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paras['post']);
}
if (@$paras['header']) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
if (@$paras['cookie']) {
curl_setopt($ch, CURLOPT_COOKIE, $paras['cookie']);
}
if (@$paras['refer']) {
if ($paras['refer'] == 1) {
curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
} else {
curl_setopt($ch, CURLOPT_REFERER, $paras['refer']);
}
}
if (@$paras['ua']) {
curl_setopt($ch, CURLOPT_USERAGENT, $paras['ua']);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
}
if (@$paras['nobody']) {
curl_setopt($ch, CURLOPT_NOBODY, 1);
}
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (@$paras['GetCookie']) {
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all("/Set-Cookie: (.*?);/m", $result, $matches);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $headerSize); //状态码
$body = substr($result, $headerSize);
$ret = [
"Cookie" => $matches, "body" => $body, "header" => $header, 'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
];
curl_close($ch);
return $ret;
}
$ret = curl_exec($ch);
if (@$paras['loadurl']) {
$Headers = curl_getinfo($ch);
$ret = $Headers['redirect_url'];
}
curl_close($ch);
return $ret;
}
使用方法
文档
GET访问
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com");
POST访问
echo teacher_curl("https://www.phpweb.com.cn",[
'post'=>[
'url'=>'qq.com'
]
]);
或
echo teacher_curl("https://www.phpweb.com.cn",[
'post'=>'url=qq.com'
]);
携带Cookie访问
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'cookie'=>'cookie内容'
]);
模拟访问来源Refer
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'refer'=>'https://api.oioweb.cn'
]);
模拟UseaAgent
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'ua'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
]);
文件上传
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'post'=>[
'file'=>new CURLFile(realpath("Curl.jpg"))
]
]);
或
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'post'=>new CURLFile(realpath("Curl.jpg"))
]);
获取301跳转地址
echo teacher_curl("https://mmbizurl.cn/s/RNHSo6Dek",[
'loadurl'=>1
]);
查看返回Header信息
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'header'=>1
]);
设置请求头信息
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'Header'=>[
'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cache-control: max-age=0'
]
]);
获取请求的全部信息
echo teacher_curl("https://www.phpweb.com.cn?url=qq.com",[
'post'=>[
'user'=>123456,
'pwd'=>123
],
'GetCookie'=>1
]);