标签

Honeymoon - Thomas Ng

归档

近期文章

快代理IP-私密代理-php实例化curlpost和get请求

Webman框架,本文章实现快代理IP-私密代理-php实例化curlpost和get请求

不懂的可以评论或联系我邮箱:owen@owenzhang.com

著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。

调用:

$res = KuaidailiService::instance()->curlByGet($url, $header);

KuaishouService.php代码如下:

<?php
/**
* 快代理IP服务类
*/
namespace app\service;
use support\Log;
use support\Redis;
class KuaidailiService
{
//快代理当前的IP写入redis的key
const   PROXY_REDIS_KEY = 'crawler:kuaidaili:proxy';
const   USERNAME        = "d394011111";
const   PASSWORD        = "jvg11111";
const   SECRET_ID       = "o0r7ln11111";
const   SIGNATURE       = "aios2rp911111yn1";
const   PRODUCT         = "dps";
public static ?KuaidailiService1 $_instance = null;
/**
* @return KuaidailiService|mixed
*/
public static function instance(): ?KuaidailiService1
{
if (!static::$_instance) static::$_instance = new self();
return static::$_instance;
}
/**
* @notes : 获取缓存中的代理IP+端口号 58.19.54.158:34510
* @return array
* @author: OwenZhang
* @time  : 2026/1/30 10:56
*/
public function getProxyByRedis(): array
{
$proxy = Redis::get(self::PROXY_REDIS_KEY);
if (!$proxy) {
$proxy = $this->getProxy();
if ($proxy['code'] != 200) {
(Log::channel('crawlerlog'))->info($proxy['msg'], []);
return ['code' => 400, 'data' => '', 'msg' => $proxy['msg']];
}
$proxy = $proxy['data'];
}
return ['code' => 200, 'data' => $proxy, 'msg' => ''];
}
/**
* @notes : 获取代理IP+端口号 58.19.54.158:34510
* @return array
* @author: OwenZhang
* @time  : 2026/1/28 11:12
*/
public function getProxy(): array
{
try {
$username  = self::USERNAME;
$password  = self::PASSWORD;
$secret_id = self::SECRET_ID;
$signature = self::SIGNATURE;
$product   = self::PRODUCT;
// 构建请求的URL,不包括认证信息,因为认证信息将添加到请求头中
// 接口文档https://www.kuaidaili.com/doc/api/getdps/
//https://dps.kdlapi.com/api/getdps/?secret_id=o0r7111115es&signature=aios2r1111voewdsyn1&num=1&format=text&sep=1&f_et=1
$url = "https://" . $product . ".kdlapi.com/api/get" . $product . "/?secret_id=" . $secret_id . "&signature=" . $signature . "&num=1&format=text&sep=1&f_et=1";
// 初始化cURL会话来获取代理服务器的IP和端口
$ch = curl_init($url);
// 创建用于基本认证的请求头
$auth = base64_encode("$username:$password");
// 设置cURL选项来发送请求并获取代理信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 修复SSL证书问题 cURL Error1: SSL certificate problem: unable to get local issuer certificateWorker process terminated
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Basic $auth",
"secret_id: $secret_id",
"signature: $signature"
]);
// 设置超时时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
// 执行请求并获取响应
$response = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
curl_close($ch);
(Log::channel('crawlerlog'))->info("cURL Error1: " . $error_msg, []);
return ['code' => 400, 'data' => '', 'msg' => "cURL Error1: " . $error_msg];
}
// 获取HTTP状态码
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 关闭cURL会话
curl_close($ch);
// 检查响应是否有效
if ($http_code != 200 || empty($response)) {
(Log::channel('crawlerlog'))->info("获取代理失败,HTTP状态码: " . $http_code . ",响应: " . $response, []);
return ['code' => 400, 'data' => '', 'msg' => "获取代理失败,HTTP状态码: " . $http_code . ",响应: " . $response];
}
// 处理响应(去除可能的空格和换行符)
// 验证响应格式是否为 IP:PORT,TTL (ip+端口,可用时间(秒))('58.19.54.158:34510,120')
$response = trim($response);
if (!strpos($response, ':') && !strpos($response, ',')) {
(Log::channel('crawlerlog'))->info("代理格式错误: " . $response, []);
return ['code' => 400, 'data' => '', 'msg' => "代理格式错误: " . $response];
}
//处理{"code":-131,"msg":"Invalid or expired order","data":""}
if (strpos($response, 'code')) {
(Log::channel('crawlerlog'))->info("KuaidailiService.getProxy.代理格式错误: " . $response, []);
return ['code' => 400, 'data' => $response, 'msg' => "KuaidailiService.getProxy.代理格式错误: " . $response];
}
list($proxy, $ttl) = explode(',', $response);
list($ip, $port) = explode(':', $proxy);
$proxy = $ip . ":" . $port;
// 验证IP和端口
if (!filter_var($ip, FILTER_VALIDATE_IP) || !is_numeric($port) || $port < 1 || $port > 65535) {
(Log::channel('crawlerlog'))->info("代理IP或端口无效: " . $proxy, []);
return ['code' => 400, 'data' => '', 'msg' => "代理IP或端口无效: " . $proxy];
}
//ip写入redis,
$proxyRedisKey = self::PROXY_REDIS_KEY;
$res           = Redis::set($proxyRedisKey, $proxy, 'EX', $ttl, 1);
if (!$res) {
(Log::channel('crawlerlog'))->info("代理IP写入redis失败: " . $response, []);
return ['code' => 400, 'data' => '', 'msg' => "代理IP写入redis失败: " . $response];
}
return ['code' => 200, 'data' => $proxy, 'msg' => ''];
} catch (\Throwable $e) {
(Log::channel('crawlerlog'))->info('KuaishouService getProxy throwable error: ' . $e->getMessage(), []);
return ['msg' => 'KuaishouService getProxy throwable error: ' . $e->getMessage(), 'code' => 500, 'data' => ''];
}
}
/**
* @notes : 通过快代理使用get请求
* @param $page_url
* @param $headers
* @return array
* @author: OwenZhang
* @time  : 2026/1/28 11:23
*/
public function curlByGet($page_url, $headers): array
{
//要访问的目标页面
if (!$page_url) {
return ['code' => 400, 'data' => '', 'msg' => "curlByGet-page_url不能为空"];
}
if (!$headers) {
$headers = [
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
];
}
$username = self::USERNAME;
$password = self::PASSWORD;
$proxy = $this->getProxyByRedis();
if ($proxy['code'] != 200) {
(Log::channel('crawlerlog'))->info($proxy['msg'], []);
return ['code' => 400, 'data' => '', 'msg' => $proxy['msg']];
}
$proxy = $proxy['data'];
// 初始化cURL会话以发送带代理的最终请求
$ch = curl_init($page_url);
// 设置cURL选项来发送带代理的最终请求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 设置代理
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// 如果代理需要基本认证,设置代理的用户名和密码
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$username}:{$password}");
// 自定义header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 如果需要设置自定义cookie,取消注释下面这行并替换为您的cookie值
// curl_setopt($ch, CURLOPT_COOKIE, 'your_cookie_value');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // 使用gzip压缩传输数据让访问更快
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); // 连接超时30秒
curl_setopt($ch, CURLOPT_TIMEOUT, 60);  // 总超时5000秒
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  // 允许重定向
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);          // 最大重定向次数
// 执行最终请求并获取响应
$result = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
//存在以下情况,跳过不记录日志
//Operation timed out after 30000 milliseconds with 0 out of 0 bytes received
//Failed to connect to 122.96.255.233 port 20317 after 5014 ms: Timeout was reachedWorker process terminated
// Received HTTP code 517 from proxy after CONNECT
//OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to weibo.com:443
if ((strpos($error_msg, 'SSL_ERROR_SYSCALL') === false) || (strpos($error_msg, '30000') === false) || (strpos($error_msg, 'port') === false) || (strpos($error_msg, '517') === false)) {
(Log::channel('crawlerlog'))->info('KuaidailiService.curlByGet cURL Error2: ' . $error_msg, []);
return ['code' => 4002, 'data' => '', 'msg' => 'KuaidailiService.curlByGet cURL Error2: ' . $error_msg];
}
return ['code' => 400, 'data' => '', 'msg' => 'KuaidailiService.curlByGet cURL Error2: ' . $error_msg];
}
// 获取请求信息
$curlInfo = curl_getinfo($ch);
//接口文档地址https://www.kuaidaili.com/doc/dev/dpshttpresponse/
//{"url":"https:\/\/mon.zijieapi.com\/monitor_web\/settings\/browser-settings?bid=passport_account_api&store=1","content_type":"application\/json; charset=utf-8","http_code":200,"header_size":1243,"request_size":0,"filetime":-1,"ssl_verify_result":19,"redirect_count":0,"total_time":0.528631,"namelookup_time":5.6e-5,"connect_time":0.037255,"pretransfer_time":0.332678,"size_upload":0,"size_download":653,"speed_download":1235,"speed_upload":0,"download_content_length":653,"upload_content_length":0,"starttransfer_time":0.528486,"redirect_time":0,"redirect_url":"","primary_ip":"112.96.215.213","certinfo":[],"primary_port":20317,"local_ip":"112.118.116.919","local_port":50326,"http_version":3,"protocol":2,"ssl_verifyresult":0,"scheme":"HTTPS","appconnect_time_us":332436,"connect_time_us":37255,"namelookup_time_us":56,"pretransfer_time_us":332678,"redirect_time_us":0,"starttransfer_time_us":528486,"total_time_us":528631,"effective_method":"GET","capath":"","cainfo":""}
// 关闭cURL会话
curl_close($ch);
return ['code' => 200, 'data' => ['result' => $result, 'curlInfo' => json_encode($curlInfo)], 'msg' => ''];
}
/**
* @notes : 通过快代理使用post请求
* @param $page_url
* @param $headers
* @param $data
* @return array
* @author: OwenZhang
* @time  : 2026/1/28 11:23
*/
public function curlByPost($page_url, $headers, $data): array
{
//要访问的目标页面
if (!$page_url) {
return ['code' => 400, 'data' => '', 'msg' => "curlByPost-page_url不能为空"];
}
if (!$headers) {
$headers = [
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
];
}
$username = self::USERNAME;
$password = self::PASSWORD;
$proxy = $this->getProxyByRedis();
if ($proxy['code'] != 200) {
(Log::channel('crawlerlog'))->info($proxy['msg'], []);
return ['code' => 400, 'data' => '', 'msg' => $proxy['msg']];
}
$proxy = $proxy['data'];
// 初始化cURL会话以发送带代理的最终请求
$ch = curl_init();
// 设置cURL选项来发送带代理的最终请求
curl_setopt($ch, CURLOPT_URL, $page_url);
curl_setopt($ch, CURLOPT_HEADER, false);//不返回头部信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$UserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3)';
curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// 设置代理
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// 如果代理需要基本认证,设置代理的用户名和密码
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$username}:{$password}");
// 自定义header
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 如果需要设置自定义cookie,取消注释下面这行并替换为您的cookie值
// curl_setopt($ch, CURLOPT_COOKIE, 'your_cookie_value');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip'); // 使用gzip压缩传输数据让访问更快
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  // 允许重定向
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);          // 最大重定向次数
// 执行最终请求并获取响应
$result = curl_exec($ch);
// 检查是否有错误发生
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
//存在以下情况,跳过不记录日志
//Operation timed out after 30000 milliseconds with 0 out of 0 bytes received
//Failed to connect to 112.96.215.233 port 20317 after 5014 ms: Timeout was reachedWorker process terminated
// Received HTTP code 517 from proxy after CONNECT
if ((strpos($error_msg, 'SSL_ERROR_SYSCALL') === false) || (strpos($error_msg, '30000') === false) || (strpos($error_msg, 'port') === false) || (strpos($error_msg, '517') === false)) {
(Log::channel('crawlerlog'))->info('KuaidailiService.curlByPost cURL Error2: ' . $error_msg, []);
return ['code' => 4002, 'data' => '', 'msg' => 'KuaidailiService.curlByPost cURL Error2: ' . $error_msg];
}
return ['code' => 400, 'data' => '', 'msg' => 'KuaidailiService.curlByPost cURL Error2: ' . $error_msg];
}
// 获取请求信息
$curlInfo = curl_getinfo($ch);
//接口文档地址https://www.kuaidaili.com/doc/dev/dpshttpresponse/
//{"url":"https:\/\/mon.zijieapi.com\/monitor_web\/settings\/browser-settings?bid=passport_account_api&store=1","content_type":"application\/json; charset=utf-8","http_code":200,"header_size":1243,"request_size":0,"filetime":-1,"ssl_verify_result":19,"redirect_count":0,"total_time":0.528631,"namelookup_time":5.6e-5,"connect_time":0.037255,"pretransfer_time":0.332678,"size_upload":0,"size_download":653,"speed_download":1235,"speed_upload":0,"download_content_length":653,"upload_content_length":0,"starttransfer_time":0.528486,"redirect_time":0,"redirect_url":"","primary_ip":"112.96.215.213","certinfo":[],"primary_port":20317,"local_ip":"112.118.116.99","local_port":50326,"http_version":3,"protocol":2,"ssl_verifyresult":0,"scheme":"HTTPS","appconnect_time_us":332436,"connect_time_us":37255,"namelookup_time_us":56,"pretransfer_time_us":332678,"redirect_time_us":0,"starttransfer_time_us":528486,"total_time_us":528631,"effective_method":"GET","capath":"","cainfo":""}
// 关闭cURL会话
curl_close($ch);
return ['code' => 200, 'data' => ['result' => $result, 'curlInfo' => json_encode($curlInfo)], 'msg' => ''];
}
}

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

快代理IP-私密代理-php实例化curlpost和get请求插图

Tags: