人生若只如初见 - curl https://blog.ilingku.com/tag/curl/ PHP案例实现“万能”的短网址还原功能 https://blog.ilingku.com/archives/4/ 2026-04-07T19:57:00+08:00 常见的短网址都是通过 301 或 302 跳转的方式实现重定向到目标网站的,因此我们可以使用 PHP 的 curl_getinfo 来取得 header 中的重定向地址,也就是短网址对应的原始网址(嗯,原理就是这么简单……代码如下/*** * 万能短网址还原函数 * @param $shortUrl 短网址 * @return 原始网址 | 空(还原失败或非短网址) */function restoreUrl($shortUrl) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $shortUrl); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0'); curl_setopt($curl, CURLOPT_HEADER, true); curl_setopt($curl, CURLOPT_NOBODY, false); curl_setopt($curl, CURLOPT_TIMEOUT, 15); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curl, CURLOPT_ENCODING, 'gzip'); $data = curl_exec($curl); $curlInfo = curl_getinfo($curl); curl_close($curl); if($curlInfo['http_code'] == 301 || $curlInfo['http_code'] == 302) { return $curlInfo['redirect_url']; } return ''; }使用方法$shortUrl = 'https://url.cn/54VbB8h'; // 要还原的短网址 $orinalUrl = restoreUrl($shortUrl); if($orinalUrl) { echo "短网址 {$shortUrl} 的还原结果:{$orinalUrl}"; } else { echo "短网址还原失败"; } 分享一个自己常用的PHP万能Curl封装代码 https://blog.ilingku.com/archives/3/ 2026-04-07T19:46:00+08:00 我用Curl比较多,也一直写类似的教程,今天发现大神出了一套非常好用的Curl封装的方法,特意转过来,以备使用!<?php /** * @link https://www.ilingku.com * @msg PHPCurl封装的方法 */ 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, "code" => $header]; 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://api.baidu.com/api/beian.php?url=qq.com");POST访问echo teacher_curl("https://api.baidu.com/api/beian.php",[ 'post'=>[ 'url'=>'qq.com' ] ]);或echo teacher_curl("https://api.baidu.com/api/beian.php",[ 'post'=>'url=qq.com' ]);携带Cookie访问echo teacher_curl("https://api.baidu.com/api/beian.php?url=qq.com",[ 'cookie'=>'cookie内容' ]);模拟访问来源Referecho teacher_curl("https://api.baidu.com/api/beian.php?url=qq.com",[ 'refer'=>' ]);模拟UseaAgentecho teacher_curl("https://api.baidu.com/api/beian.php?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://api.baidu.com/api/beian.php?url=qq.com",[ 'post'=>[ 'file'=>new CURLFile(realpath("Curl.jpg")) ] ]);或:echo teacher_curl("https://api.baidu.com/api/beian.php?url=qq.com",[ 'post'=>new CURLFile(realpath("Curl.jpg")) ]);获取301跳转地址echo teacher_curl("https://baidu.com/",[ 'loadurl'=>1 ]);查看返回Header信息echo teacher_curl("https://api.baidu.com/api/beian.php?url=qq.com",[ 'header'=>1] );设置请求头信息echo teacher_curl("https://api.baidu.com/api/beian.php?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' ] ]);获取POST以后返回的Header、Body、Cookie内容echo teacher_curl("https://api.ilingku.com/api/beian.php?url=qq.com",[ 'post'=>[ 'user'=>123456, 'pwd'=>123 ], 'GetCookie'=>1 ]);