利用file_get_contents();抓取网络上需要的信息

资料

参考资料

菜鸟教程file_get_contents函数详解 官网解释
链接 链接

总体来说file_get_contents() 函数是用来将文件的内容读入到一个字符串;

2.使用场景

例如我们有需要的功能字段不需要在数据库中自建,需要在网络上爬取的时候
比如我想得到目前市面上最火的找工作标签

使用示例

这里我是用的是 laravel框架 在控制器中使用

 # 如果没有缓存执行下一步
 if (!Cache::has('workLabelName')) {
            #将读出的字符串数据`file_get_contents()得到的字符串`,使用json_decode()函数解密为对象
            $workLabel = json_decode(file_get_contents("https://www.zhipin.com/wapi/zpCommon/data/oldindustry.json"));
            # 得到想要的数据对象
            $workLabel = $workLabel->zpData;
            #把我们需要的字段使用array_column函数提取出来
            $workLabelName = array_column($workLabel, 'name');
            #加入缓存
            Cache::put('workLabelName', $workLabelName, config('cache.oneDay'));
        }
        # 分支语句:到这一步就证明有缓存所以直接从缓存里面提取我们需要的数据即可
        return $this->success(Cache::get('workLabelName'));
    }

结果示例

结果示例

原文地址:https://www.cnblogs.com/yaoliuyang/p/14639570.html