但实际上,通过上述的代码获得的图片src可能也是有问题的,或者根本没有抓取到数据。不过我们先不考虑这些问题,我们先实现本文的目标。
接下来就是关键代码一,它要实现“抓取-保存到本地”两个功能:
代码如下:
$get = wp_remote_get( $get_image_src );
$type = wp_remote_retrieve_header( $get, 'content-type' );
$file_name = basename($get_image_src);
$file_content = wp_remote_retrieve_body($get);
$mirror = wp_upload_bits($file_name,null,$file_content);
$get = wp_remote_get( $get_image_src );
$type = wp_remote_retrieve_header( $get, 'content-type' );
$file_name = basename($get_image_src);
$file_content = wp_remote_retrieve_body($get);
$mirror = wp_upload_bits($file_name,null,$file_content);
这个地方有一个变化,上面我们获得了$first_image,这个地方的$get_image_src是上述得到的要抓取的远程图片地址。
在WordPress中,提供了wp_remote_get、wp_remote_retrieve_body等原创抓取和信息获取函数,你可以查看官方文档以了解和remote相关的函数。wp_upload_bits则将抓取到的图片的二进制内容保存到本地,根据其文件类型,最终成为本地保存的图片,并将保存完后获得的本地图片信息保存在$mirror中。
既然已经保存到本地了,接下来就是将图片信息保存到数据库中。
标签:WordPress
相关阅读 >>
php-cgi进程cpu负载过高:禁用wordpress定时任务wp-cron.php
wordpress中is_sticky()判断文章是否置顶的参数与用法
在sae(sina app engine)上部署wordpress站点的教程
php中__file__、dirname与basename用法实例分析
#实践笔记#本地配置phpnow与wordpress运行环境
更多相关阅读请进入《wordpress》频道 >>
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。