質問編集履歴
1
コントローラーを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -66,4 +66,180 @@
|
|
66
66
|
|
67
67
|
```
|
68
68
|
|
69
|
+
MangaController.php
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
```ここに言語を入力
|
74
|
+
|
75
|
+
{
|
76
|
+
|
77
|
+
$keyword = $request->input('keyword');
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
$iskeyword = Manga::where('title', 'like', '%'.$keyword.'%')->first();
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
if (!$iskeyword) {
|
86
|
+
|
87
|
+
echo 'DBに新しく登録します';
|
88
|
+
|
89
|
+
// $options = [
|
90
|
+
|
91
|
+
// 'http' => [
|
92
|
+
|
93
|
+
// 'method' => 'GET',
|
94
|
+
|
95
|
+
// 'header' => 'user-agent:MyUserAgent'
|
96
|
+
|
97
|
+
// ],
|
98
|
+
|
99
|
+
// ];
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
$RakutenUrl = "https://app.rakuten.co.jp/services/api/BooksBook/Search/20170404?applicationId=1064175175388514312&title={$keyword}&booksGenreId=001&size=9";
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
// $json = file_get_contents($RakutenUrl, false, stream_context_create($options));
|
108
|
+
|
109
|
+
$json = file_get_contents($RakutenUrl);
|
110
|
+
|
111
|
+
// dd($json);
|
112
|
+
|
113
|
+
$json_decode = json_decode($json, true);
|
114
|
+
|
115
|
+
// dd($json_decode);
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
$res = [];
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
foreach ($json_decode['Items'] as $item) {
|
124
|
+
|
125
|
+
$title = $item['Item']['title'];
|
126
|
+
|
127
|
+
$ImageUrl = $item['Item']['largeImageUrl'];
|
128
|
+
|
129
|
+
$source_url = $item['Item']['itemUrl'];
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
$res[] = [
|
134
|
+
|
135
|
+
'title' => $title,
|
136
|
+
|
137
|
+
'ImageUrl' => $ImageUrl,
|
138
|
+
|
139
|
+
'itemUrl' => $source_url,
|
140
|
+
|
141
|
+
];
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
// dd($res);
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
// return $res;
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
foreach ($res as $i) {
|
154
|
+
|
155
|
+
$manga = new Manga;
|
156
|
+
|
157
|
+
$manga->title = $i['title'];
|
158
|
+
|
159
|
+
$manga->img_url = $i['ImageUrl'];
|
160
|
+
|
161
|
+
$manga->source_url = $i['itemUrl'];
|
162
|
+
|
163
|
+
$manga->save();
|
164
|
+
|
165
|
+
};
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
$getmanga = Manga::where('title', 'like', '%'.$keyword.'%')->get();
|
170
|
+
|
171
|
+
// dd($getmanga);
|
172
|
+
|
173
|
+
foreach ($getmanga as $m) {
|
174
|
+
|
175
|
+
$title = $m->title;
|
176
|
+
|
177
|
+
$ImageUrl = $m->img_url;
|
178
|
+
|
179
|
+
$source_url = $m->source_url;
|
180
|
+
|
181
|
+
$result[] = [
|
182
|
+
|
183
|
+
'title' => $title,
|
184
|
+
|
185
|
+
'img_url' => $ImageUrl,
|
186
|
+
|
187
|
+
'source_url' => $source_url
|
188
|
+
|
189
|
+
];
|
190
|
+
|
191
|
+
};
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
// dd($result);
|
196
|
+
|
197
|
+
return $result;
|
198
|
+
|
199
|
+
// }
|
200
|
+
|
201
|
+
} elseif ($keyword) {
|
202
|
+
|
203
|
+
echo 'DBに登録されています';
|
204
|
+
|
205
|
+
// dd($iskeyword);
|
206
|
+
|
207
|
+
$searchmangas = Manga::where('title', 'like', '%'.$keyword.'%')->get();
|
208
|
+
|
209
|
+
// dd($searchmangas);
|
210
|
+
|
211
|
+
$result2 = [];
|
212
|
+
|
213
|
+
foreach ($searchmangas as $m) {
|
214
|
+
|
215
|
+
$title = $m->title;
|
216
|
+
|
217
|
+
$ImageUrl = $m->img_url;
|
218
|
+
|
219
|
+
$source_url = $m->source_url;
|
220
|
+
|
221
|
+
$result2[] = [
|
222
|
+
|
223
|
+
'title' => $title,
|
224
|
+
|
225
|
+
'img_url' => $ImageUrl,
|
226
|
+
|
227
|
+
'source_url' => $source_url
|
228
|
+
|
229
|
+
];
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
return result2;
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
}
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
69
245
|
よろしくお願いします。
|