Skip to content

Commit 6ee55c7

Browse files
author
JinYi Chen
committed
初始化
0 parents  commit 6ee55c7

File tree

5 files changed

+189
-0
lines changed

5 files changed

+189
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

Config.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/7/31
6+
* Time: 下午8:53
7+
*/
8+
//定义目录名
9+
define("API_PATH","Api"); //存放API Key文件夹
10+
define("FILE_PATH","Resource"); //存放文件文件夹名
11+
12+
//网站API链接
13+
define("PIXABAY_API_URL","https://pixabay.com/api/");

Spider.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/7/31
6+
* Time: 下午9:01
7+
*/
8+
9+
function print_dir($dir_path)
10+
{
11+
$files = array();
12+
if (@$handle = opendir($dir_path)) { //注意这里要加一个@,不然会有warning错误提示:)
13+
while (($file = readdir($handle)) !== false) {
14+
if ($file != ".." && $file != ".") { //排除根目录;
15+
$files[] = $file;
16+
}
17+
}
18+
closedir($handle);
19+
return $files;
20+
}
21+
}
22+
$dir=print_dir('src');
23+
24+
$spider="";
25+
foreach ($dir as $path ){
26+
27+
}
28+
29+
$print ="
30+
=============================
31+
PHP Images Spider
32+
"
33+
.$spider.
34+
"
35+
Chenjinyi:https://github.com/Chenjinyi
36+
=============================
37+
";

src/PublicCore.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/7/31
6+
* Time: 下午8:39
7+
*/
8+
9+
require_once "../Config.php";
10+
11+
class PublicCore
12+
{
13+
/**
14+
* CURL GET请求
15+
* @param $url string 请求URL
16+
* @return mixed 返回获取信息
17+
*/
18+
public function curl_get($url)
19+
{
20+
$ch = curl_init(); //初始化一个cURL会话
21+
curl_setopt($ch, CURLOPT_URL, $url);//设置需要获取的 URL 地址
22+
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
23+
"Host:pixabay.com",
24+
"Connection: keep-alive",
25+
"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
26+
"Upgrade-Insecure-Requests: 1",
27+
"DNT:1",
28+
"Accept-Language:zh-CN,zh;q=0.8",
29+
"User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"
30+
)); // 设置浏览器的特定header
31+
32+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//不返回数据
33+
34+
if (empty($result = curl_exec($ch))) {
35+
print_r('无法连接' . $url);
36+
die();
37+
};//执行一个cURL会话
38+
return $result;
39+
}
40+
41+
/**
42+
* 下载图片(单线程)多线程版容易请求太频繁
43+
* @param $file_url array 下载链接-文件名
44+
* @param $dir_name string 保存的文件夹
45+
*/
46+
public function image_save($file_url, $dir_name)
47+
{ //下载
48+
foreach ($file_url as $url) {
49+
$image_name = array_keys($file_url);
50+
$url = "http://" . $url;
51+
52+
if (file_exists($dir_name . DIRECTORY_SEPARATOR . $image_name)) {//检测是否存在
53+
echo "已存在" . PHP_EOL;
54+
continue;
55+
} else {
56+
if ($image_save = file_get_contents($url)) {
57+
@file_put_contents($dir_name . DIRECTORY_SEPARATOR . $image_name, $image_save);
58+
} else {
59+
print_r("下载错误:" . $url);
60+
}
61+
}
62+
}
63+
}
64+
/**
65+
* 创建文件夹
66+
* @param $dir_name string 文件夹名
67+
*/
68+
public function dir_create($dir_name)
69+
{
70+
if (!file_exists($dir_name)) {
71+
mkdir($dir_name, 0777, true);//创建文件夹
72+
}
73+
}
74+
75+
/**
76+
* 获取提示并用户输入
77+
* @param $string
78+
* @return string
79+
*/
80+
public function user_input($string)
81+
{
82+
print_r($string);
83+
$input = trim(fgets(STDIN));
84+
return $input;
85+
}
86+
87+
/**
88+
* 初始化文件夹
89+
*/
90+
public function init_dir()
91+
{
92+
$this->dir_create(API_PATH);
93+
$this->dir_create(FILE_PATH);
94+
}
95+
96+
/**
97+
* 确认API文件存在
98+
* @param $filename
99+
* @return bool|string
100+
*/
101+
public function check_api_file($filename)
102+
{
103+
$file_path=API_PATH.DIRECTORY_SEPARATOR.$filename;
104+
if (!file_exists($file_path)){
105+
touch($file_path);
106+
}
107+
$file=file_get_contents($file_path);
108+
if (!empty($file)){
109+
return $file;
110+
}
111+
return false;
112+
}
113+
}

src/spider/PixabayCore.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: jinyi
5+
* Date: 2018/7/30
6+
* Time: 下午1:10
7+
*/
8+
9+
require_once "../PublicCore.php";
10+
11+
class PixabayCore
12+
{
13+
14+
}
15+
16+
$spiderCore = new PublicCore();
17+
$spiderCore->init_dir();
18+
$key = $spiderCore->check_api_file('PixabayApiKey') ?: die("PixabayKey为空");
19+
$q = $spiderCore->user_input("请输入一个需要查询的字符串");
20+
$result = json_decode($result = $spiderCore->curl_get(PIXABAY_API_URL."?key=".$key."&q=".$q));
21+
$images_arr=[];
22+
foreach ($result['hits'] as $images){
23+
$format=explode('.',$images['largeImageURL']);
24+
array_push($images_arr,[$images['id'].$format['1']=>$images['largeImageURL']]);
25+
}
26+

0 commit comments

Comments
 (0)