最近博客用的图床禁止大陆IP访问了,无奈找了本地化插件,无奈找到都是发布时外链本地化,所以自己写了个
因为禁止大陆IP访问,通过嫖来的一个代理挂着,写了个curl下载时发现下载的文件都是0KB,没有下载成功,但通过浏览器访问确实能访问到,怀疑是php没走代理,想着用别的下载软件下一个图片试试,果然,下载不到,加了代理就行。
所以在CURL上使用代理,成功下载,nice。
上代码:
<?php
set_time_limit(0);
ini_set(‘max_execution_time’, ‘0’);
function db()
{
try {
$db = new PDO(“mysql:host=localhost;dbname=blog”, “root”, “root”);
//$db = new PDO(“mysql:host=localhost;dbname=entboos”,”root”,”root”,array(PDO::ATTR_PERSISTENT => true));//长连接
return $db;
} catch (PDOException $e) {
die(“数据库连接失败” . $e->getMessage());
}
}
$res = db()->prepare(‘select * from posts’);
$res->execute();
$res = $res->fetchall();
if (!is_dir(‘wp-content’)) {
mkdir(‘wp-content’);
}
if (!is_dir(‘wp-content/uploads’)) {
mkdir(‘wp-content/uploads’);
}
foreach ($res as $key => $item) {
$pageContents = $item[‘post_content’];
$pageContents = str_replace(‘\”‘, ‘”‘, $pageContents);
$reg = ‘/<img (.*?)+src=[\'”]https:\/\/upload.cc\/i1\/(.*?)[\'”]/i’; //正则读取数据库编辑器写文本中的图片路径
preg_match_all($reg, $pageContents, $results);
//打印出来
if (!empty($results[2])) {
foreach ($results[2] as $k => $v) {
$arr = explode(‘/’, $v);
//文件夹创建
if (!is_dir(‘wp-content/uploads/’ . $arr[0])) {
@mkdir(‘wp-content/uploads/’ . $arr[0]);
}
if (!is_dir(‘wp-content/uploads/’ . $arr[0] . ‘/’ . $arr[1])) {
@mkdir(‘wp-content/uploads/’ . $arr[0] . ‘/’ . $arr[1]);
}
if (!is_dir(‘wp-content/uploads/’ . $arr[0] . ‘/’ . $arr[1] . ‘/’ . $arr[2])) {
@mkdir(‘wp-content/uploads/’ . $arr[0] . ‘/’ . $arr[1] . ‘/’ . $arr[2]);
}
$path = ‘wp-content/uploads/’ . $arr[0] . ‘/’ . $arr[1] . ‘/’ . $arr[2] . ‘/’ . $arr[3];//路径拼接
$arrRes[“https://upload.cc/i1/” . $v] = “https://upload.cc/i1/” . $v;
if (!file_exists($path)) {
try {
$fp = fopen($path, ‘w’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, “https://upload.cc/i1/” . $v);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_PROXY, ‘127.0.0.1:8001’); //设置代理
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FILE, $fp);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
fclose($fp);
$size = filesize($path);
if ($size != $info[‘size_download’]) {
echo $arrRes[“https://upload.cc/i1/” . $v] . “下载失败\n”;
$arrRes[“https://upload.cc/i1/” . $v] = $arrRes[“https://upload.cc/i1/” . $v] . ‘—>下载失败<hr>’;
} else {
echo $arrRes[“https://upload.cc/i1/” . $v] . “下载成功\n”;
$arrRes[“https://upload.cc/i1/” . $v] = $arrRes[“https://upload.cc/i1/” . $v] . ‘—>下载成功<hr>’;
}
curl_close($ch);
} catch (Exception $e) {
echo $arrRes[“https://upload.cc/i1/” . $v] . “下载失败\n”;
$arrRes[“https://upload.cc/i1/” . $v] = $arrRes[“https://upload.cc/i1/” . $v] . ‘—>下载失败’ . $e->getMessage() . ‘<hr>’;
}
}
sleep(2);
}
}
}
echo “\n\n\n\n\n\n\n”;
print_r($arrRes);
数据库url替换
<?php
set_time_limit(0);
ini_set(‘max_execution_time’, ‘0’);
function db()
{
try {
$db = new PDO(“mysql:host=localhost;dbname=blog”, “root”, “root”);
//$db = new PDO(“mysql:host=localhost;dbname=entboos”,”root”,”root”,array(PDO::ATTR_PERSISTENT => true));//长连接
return $db;
} catch (PDOException $e) {
die(“数据库连接失败” . $e->getMessage());
}
}
$res = db()->prepare(‘select * from posts’);
$res->execute();
$res = $res->fetchall();
foreach ($res as $key => $item) {
// $pageContents = str_replace(‘\”‘, ‘”‘, $pageContents);
// $reg = ‘/<img (.*?)+src=[\'”]https:\/\/upload.cc\/i1\/(.*?)[\'”]/i’; //正则读取数据库编辑器写文本中的图片路径
// preg_match_all($reg, $pageContents, $results);
if (strpos($item[‘post_content’], ‘https://upload.cc/i1/’) !== false) {
$item[‘post_content’] = str_replace(‘https://upload.cc/i1/’,’/wp-content/uploads/’,$item[‘post_content’]);
$sql=”update posts set post_content=? where ID=?”;
$res = db()->prepare($sql);
$res->bindValue(1,$item[‘post_content’]);//对应第一个问号
$res->bindValue(2,$item[‘ID’]);//对应第三个问号
$res->execute();
$res = $res->rowCount();
echo $item[‘ID’] . ‘=====>’ . $res.”\n”;
} else {
echo ‘没有使用upload外链,id:’.$item[‘ID’].”\n”;
}
}
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: PHPCURL通过代理下载文件,wordpress远程图片本地化
本文地址: PHPCURL通过代理下载文件,wordpress远程图片本地化