質問編集履歴

1

データを直しました。

2019/07/04 07:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,22 +1,26 @@
1
1
  PHP初心者です。
2
2
 
3
- PHPでtwitterのデータをjsonでまとめています。
4
-
5
- apiでデータを取得、jsonにFILE_APPENDで追記する際に同じデータがあった場合一つにしたいです。
3
+ PHPでtwitterのデータをjsonでまとめています。apiでデータを取得、jsonにFILE_APPENDで追記する際に同じデータがあった場合一つにしたいです。
6
-
7
-
8
4
 
9
5
  ### 前提・実現したいこと
10
6
 
11
-
7
+ ↓文字制限のため、一部割愛
12
8
 
13
9
  ```JSON
14
10
 
15
11
  {
16
12
 
13
+ "statuses": [
14
+
15
+ {
16
+
17
- "created_at": "Thu Jul 04 05:15:03 +0000 2019",
17
+ "created_at": "Thu Jul 04 05:19:33 +0000 2019",
18
+
18
-
19
+ "id": "1146649726030249984",
20
+
21
+ "id_str": "1146649726030249984",
22
+
19
- "text": "#テスト リンゴ",
23
+ "text": "#テスト\n狭山市\nイチ農家があります",
20
24
 
21
25
  "truncated": false,
22
26
 
@@ -68,13 +72,43 @@
68
72
 
69
73
  "in_reply_to_screen_name": null,
70
74
 
71
-
75
+ "user": {
76
+
72
-
77
+ "id": "1075985828399046656",
78
+
79
+ "id_str": "1075985828399046656",
80
+
81
+ "name": "略,
82
+
83
+ "screen_name": "略",
84
+
85
+ "location": "",
86
+
87
+ "description": "略",
88
+
89
+ "url": null,
90
+
91
+ "entities": {
92
+
93
+ "description": {
94
+
95
+ "urls": []
96
+
97
+ }
98
+
99
+ }{
100
+
101
+ "statuses": [
102
+
73
- {
103
+ {
74
-
104
+
75
- "created_at": "Thu Jul 04 05:15:03 +0000 2019",
105
+ "created_at": "Thu Jul 04 05:19:33 +0000 2019",
106
+
76
-
107
+ "id": "1146649726030249984",
108
+
109
+ "id_str": "1146649726030249984",
110
+
77
- "text": "#テスト リンゴ",
111
+ "text": "#テスト\n狭山市\nイチ農家があります",
78
112
 
79
113
  "truncated": false,
80
114
 
@@ -126,138 +160,224 @@
126
160
 
127
161
  "in_reply_to_screen_name": null,
128
162
 
129
-
163
+ "user": {
164
+
165
+ "id": "1075985828399046656",
166
+
167
+ "id_str": "1075985828399046656",
168
+
169
+ "name": "略",
170
+
171
+ "screen_name": "略",
172
+
173
+ "location": "",
174
+
175
+ "description": "略",
176
+
177
+ "url": null,
178
+
179
+ "entities": {
180
+
181
+ "description": {
182
+
183
+ "urls": []
184
+
185
+ }
186
+
187
+ }
188
+
189
+ ```
190
+
191
+ 上記のように重複しているデータあり、
192
+
193
+ ↓正しいデータ
194
+
195
+ ```JSON
196
+
197
+
198
+
199
+ {
200
+
201
+ "statuses": [
130
202
 
131
203
  {
132
204
 
133
-
205
+ "created_at": "Thu Jul 04 05:19:33 +0000 2019",
206
+
207
+ "id": "1146649726030249984",
208
+
209
+ "id_str": "1146649726030249984",
210
+
211
+ "text": "#テスト\n狭山市\nイチゴ農家があります",
212
+
213
+ "truncated": false,
214
+
215
+ "entities": {
216
+
217
+ "hashtags": [
218
+
219
+ {
220
+
221
+ "text": "テスト",
222
+
223
+ "indices": [
224
+
225
+ 0,
226
+
227
+ 7
228
+
229
+ ]
230
+
231
+ }
232
+
233
+ ],
234
+
235
+ "symbols": [],
236
+
237
+ "user_mentions": [],
238
+
239
+ "urls": []
240
+
241
+ },
242
+
243
+ "metadata": {
244
+
245
+ "iso_language_code": "ja",
246
+
247
+ "result_type": "recent"
248
+
249
+ },
250
+
251
+ "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>",
252
+
253
+ "in_reply_to_status_id": null,
254
+
255
+ "in_reply_to_status_id_str": null,
256
+
257
+ "in_reply_to_user_id": null,
258
+
259
+ "in_reply_to_user_id_str": null,
260
+
261
+ "in_reply_to_screen_name": null,
262
+
263
+ "user": {
264
+
265
+ "id": "1075985828399046656",
266
+
267
+ "id_str": "1075985828399046656",
268
+
269
+ "name": "略",
270
+
271
+ "screen_name": "略",
272
+
273
+ "location": "",
274
+
275
+ "description": "略",
276
+
277
+ "url": null,
278
+
279
+ "entities": {
280
+
281
+ "description": {
282
+
283
+ "urls": []
284
+
285
+ }
286
+
287
+ },
288
+
289
+ "protected": false,
290
+
291
+ "followers_count": 1,
292
+
293
+ "friends_count": 3,
294
+
295
+ "listed_count": 0,
296
+
297
+ "created_at": "Fri Dec 21 05:26:27 +0000 2018",
298
+
299
+ "favourites_count": 1,
300
+
301
+ "utc_offset": null,
302
+
303
+ "time_zone": null,
304
+
305
+ "geo_enabled": false,
306
+
307
+ "verified": false,
308
+
309
+ "statuses_count": 49,
310
+
311
+ "lang": null,
312
+
313
+ "contributors_enabled": false,
314
+
315
+ "is_translator": false,
316
+
317
+ "is_translation_enabled": false,
318
+
319
+ "profile_background_color": "F5F8FA",
320
+
321
+ "profile_background_image_url": null,
322
+
323
+ "profile_background_image_url_https": null,
324
+
325
+ "profile_background_tile": false,
326
+
327
+ "profile_image_url": "http://pbs.twimg.com/profile_images/1075989853701984256/cfTYj2mr_normal.jpg",
328
+
329
+ "profile_image_url_https": "https://pbs.twimg.com/profile_images/1075989853701984256/cfTYj2mr_normal.jpg",
330
+
331
+ "profile_link_color": "1DA1F2",
332
+
333
+ "profile_sidebar_border_color": "C0DEED",
334
+
335
+ "profile_sidebar_fill_color": "DDEEF6",
336
+
337
+ "profile_text_color": "333333",
338
+
339
+ "profile_use_background_image": true,
340
+
341
+ "has_extended_profile": true,
342
+
343
+ "default_profile": true,
344
+
345
+ "default_profile_image": false,
346
+
347
+ "following": false,
348
+
349
+ "follow_request_sent": false,
350
+
351
+ "notifications": false,
352
+
353
+ "translator_type": "none"
354
+
355
+ },
356
+
357
+
134
358
 
135
359
  ```
136
360
 
137
- のように同じデータがjsonあっとして、これを
361
+ 上のように一つまとめい。
362
+
363
+
364
+
138
-
365
+ ### 該当のソースコード
366
+
367
+
368
+
139
- ```JSON
369
+ ```PHP
140
-
141
- {
370
+
142
-
143
- "created_at": "Thu Jul 04 05:15:03 +0000 2019",
144
-
145
- "text": "#テスト リンゴ",
146
-
147
- "truncated": false,
371
+ <?php
148
-
372
+
373
+
374
+
149
- "entities": {
375
+ //文字制限でAPIは割愛
150
-
151
- "hashtags": [
376
+
152
-
153
- {
154
-
155
- "text": "テスト",
156
-
157
- "indices": [
158
-
159
- 0,
160
-
161
- 7
162
-
163
- ]
164
-
165
- }
166
-
167
- ],
168
-
169
- "symbols": [],
170
-
171
- "user_mentions": [],
172
-
173
- "urls": []
174
-
175
- },
176
-
177
- "metadata": {
178
-
179
- "iso_language_code": "ja",
377
+ $json = json_encode($tweet, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
180
-
378
+
181
- "result_type": "recent"
379
+ file_put_contents('./tweets.json', $json, FILE_APPEND);
182
-
380
+
183
- },
381
+ ?>
184
-
185
- "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>",
186
-
187
- "in_reply_to_status_id": null,
188
-
189
- "in_reply_to_status_id_str": null,
190
-
191
- "in_reply_to_user_id": null,
192
-
193
- "in_reply_to_user_id_str": null,
194
-
195
- "in_reply_to_screen_name": null,
196
-
197
-
198
-
199
- {
200
382
 
201
383
  ```
202
-
203
- 上のように一つにまとめたい。
204
-
205
-
206
-
207
- ### 該当のソースコード
208
-
209
-
210
-
211
- ```PHP
212
-
213
- <?php
214
-
215
-
216
-
217
- require 'vendor/autoload.php';
218
-
219
- use Abraham\TwitterOAuth\TwitterOAuth;
220
-
221
-
222
-
223
- $consumer_key = '略';
224
-
225
- $consumer_secret = '略';
226
-
227
- $access_token = '略';
228
-
229
- $access_token_secret = '略';
230
-
231
- $google_api = '略';
232
-
233
-
234
-
235
- $connection = new twitteroauth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
236
-
237
-
238
-
239
- $tweet = $connection->get("search/tweets", array("lang" => "ja",
240
-
241
- "q"=>"#テスト",
242
-
243
- 'exclude:retweets' => 'true',
244
-
245
- 'user_mentions' => 'true',
246
-
247
- 'exclude_replies' => 'true',
248
-
249
- 'text' => 'utf-8',
250
-
251
- 'count'=>10));
252
-
253
-
254
-
255
-
256
-
257
- $json = json_encode($tweet, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
258
-
259
- file_put_contents('./tweets.json', $json, FILE_APPEND);
260
-
261
- ?>
262
-
263
- ```