Fetch the repository succeeded.
<?php
/**
* Curl助手函数
*/
class CurlHelper
{
/**
* 发起get|post请求
*
* @param $url 请求地址
* @param string $type 请求类型:get|post
* @param array $data 请求数据,若存在header,则传值参考:data=['data' => json传值数据,'header' => header传值数组]
* @return bool|mixed
*/
public static function curlHttp($url, $type = 'get', $data = [])
{
$curl = curl_init();
$header = [];
if ($type == 'post') {
if (isset($data['header'])) {
extract($data);
}
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
} else {
$url .= '?' . http_build_query($data);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
list($content, $status) = array(curl_exec($curl), curl_getinfo($curl), curl_close($curl));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* 发起一个post json数据请求
*
* @param $url 请求地址
* @param string $data_json json串
* @return bool|mixed
* 调用示例:CurlHelper::curlJsonHttp($url, $data_json);
* 接收端获取json方法:$request_data = json_decode(file_get_contents('php://input'), true);
*/
public static function curlJsonHttp($url, $data_json)
{
$header = [
"Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data_json)
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
list($content, $status) = array(curl_exec($ch), curl_getinfo($ch), curl_close($ch));
return (intval($status["http_code"]) === 200) ? $content : false;
}
/**
* 多线程请求
*
* @param array $data 格式:[['url' => $url, 'params' => $params],['url' => $url, 'params' => $params]]
* @param string $method 请求方式:get|post
* @return bool|string
*
* 调用示例:CurlHelper::mFetch($data, 'get');
*/
public static function mFetch($data, $method = 'get')
{
if (empty($data)) {
return false;
}
$ch = [];
$ret = [];
$mh = curl_multi_init();
foreach ($data as $key => $item) {
$ch[$key] = curl_init();
$url = $item['url'];
if (strtolower($method) === 'get') {
$url .= '?' . http_build_query($item['params']);
} else {
curl_setopt($ch[$key], CURLOPT_POSTFIELDS, $item['params']);
}
curl_setopt($ch[$key], CURLOPT_URL, $url);
curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch[$key], CURLOPT_TIMEOUT, 30);
//curl_setopt($ch[$key], CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch[$key], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36');
if (isset($_SERVER['HTTP_USER_AGENT'])) {
curl_setopt($ch[$key], CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
curl_multi_add_handle($mh, $ch[$key]);
}
$active = null;
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
foreach ($ch as $k => $v) {
if (curl_error($v) == "") {
$ret[$k] = curl_multi_getcontent($v);
}
curl_multi_remove_handle($mh, $v);
unset($ch[$k]);
}
curl_multi_close($mh);
return $ret;
}
/**
* 获取客户端IP地址
*
* @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
* @param boolean $adv 是否进行高级模式获取(有可能被伪装)
* @return mixed
*/
public static function getClientIP($type = 0, $adv = false)
{
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if ($adv) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) unset($arr[$pos]);
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法验证
$long = sprintf("%u", ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
/**
* 是否是HTTPS请求
* @return bool
*/
public static function isHttps()
{
if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
return true;
} elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
return true;
} elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
return true;
}
return false;
}
/**
* 是否是微信
* @return bool
*/
public static function isWeixin()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
/**
* 检查是否是以手机浏览器进入(IN_MOBILE)
*/
public static function isMobile()
{
$mobile = array();
static $mobilebrowser_list = 'Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
// note 获取手机浏览器
if (preg_match("/$mobilebrowser_list/i", $_SERVER ['HTTP_USER_AGENT'], $mobile)) {
return true;
} else {
if (preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER ['HTTP_USER_AGENT'])) {
return false;
} else {
if ($_GET ['mobile'] === 'yes') {
return true;
} else {
return false;
}
}
}
}
/**
* 检测是否是iPhone设备
* @return bool
*/
public static function isiPhone()
{
return strpos($_SERVER ['HTTP_USER_AGENT'], 'iPhone') !== false;
}
/**
* 检测是否是iPad设备
* @return bool
*/
public static function isiPad()
{
return strpos($_SERVER ['HTTP_USER_AGENT'], 'iPad') !== false;
}
/**
* 检测是否是iOS系统
* @return bool
*/
public static function isiOS()
{
return isiPhone() || isiPad();
}
/**
* 检测是否是Android系统
* @return bool
*/
public static function isAndroid()
{
return strpos($_SERVER ['HTTP_USER_AGENT'], 'Android') !== false;
}
}
//$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse", 'd' => array("e" => "Cat", "f" => "Dog", "g" => "Horse"));
//extract($my_array);
//var_dump($d);
//echo CurlHelper::getClientIP();
//var_dump(CurlHelper::isHttps());
//var_dump(CurlHelper::isWeixin());
//$url = 'http://localhost/test.php';
# get请求
//echo CurlHelper::curlHttp($url, 'get', ['id' => mt_rand(1, 10)]);
# post请求
//echo CurlHelper::curlHttp($url, 'post', ['id' => mt_rand(1, 10)]);
# post json请求
//$data_json = json_encode(['id' => mt_rand(1, 10)]);
//$header = [
// "Content-Type: application/json; charset=utf-8",
// "Content-Length: " . strlen($data_json)
//];
//echo CurlHelper::curlHttp($url, 'post', ['data' => $data_json, 'header' => $header]);
//echo CurlHelper::curlJsonHttp($url, $data_json);
# 多线程请求实例
$data = [];
for ($i = 0; $i < 10; $i++) {
$item = [
'url' => 'http://localhost/test.php',
'params' => ['id' => $i + 1],
];
$data[] = $item;
}
$startTime = microtime(true);
$ret = CurlHelper::mFetch($data, 'get');
$endTime = microtime(true);
echo $endTime - $startTime;
echo "\n";
print_r($ret);
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。