前提・実現したいこと
php初心者です。
phpからjsonデータの必要な部分だけ取り出して、sqlに一括格納したいです。
jsonにするまでには出来ているので、そこまでは略します。
tweets.json
1{ 2 "statuses": [ 3 { 4 "created_at": "略1", 5 "id": "略1", 6 "id_str": "略1", 7 "text": "#テスト\n略1。", 8 "truncated": false, 9 "entities": { 10 "hashtags": [ 11 { 12 "text": "テスト", 13 "indices": [ 14 0, 15 7 16 ] 17 } 18 ], 19 "symbols": [], 20 "user_mentions": [], 21 "urls": [] 22 }, 23 "metadata": { 24 "iso_language_code": "ja", 25 "result_type": "recent" 26 }, 27 "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", 28 "in_reply_to_status_id": null, 29 "in_reply_to_status_id_str": null, 30 "in_reply_to_user_id": null, 31 "in_reply_to_user_id_str": null, 32 "in_reply_to_screen_name": null, 33 "user": { 34 "id": "1075985828399046656", 35 "id_str": "1075985828399046656", 36 "name": "略1", 37 "screen_name": "略1", 38 "location": "", 39 "description": "略1", 40 "url": null, 41 "entities": { 42 "description": { 43 "urls": [] 44 } 45 } 46 { 47 "created_at": "略2", 48 "id": "1140596976062304256", 49 "id_str": "1140596976062304256", 50 "text": "#テスト/n略2", 51 "truncated": false, 52 "entities": { 53 "hashtags": [ 54 { 55 "text": "テスト", 56 "indices": [ 57 0, 58 7 59 ] 60 } 61 ], 62 "symbols": [], 63 "user_mentions": [], 64 "urls": [] 65 }, 66 "metadata": { 67 "iso_language_code": "ja", 68 "result_type": "recent" 69 }, 70 "source": "<a href=\"https://mobile.twitter.com\" rel=\"nofollow\">Twitter Web App</a>", 71 "in_reply_to_status_id": null, 72 "in_reply_to_status_id_str": null, 73 "in_reply_to_user_id": null, 74 "in_reply_to_user_id_str": null, 75 "in_reply_to_screen_name": null, 76 "user": { 77 "id": "1075985828399046656", 78 "id_str": "1075985828399046656", 79 "name": "略2", 80 "screen_name": "略2", 81 "location": "", 82 "description": "略2", 83 "url": null, 84 "entities": { 85 "description": { 86 "urls": [] 87 } 88 }
このようにデータを持っていて(ツイートデータで長いので略してます)、そこから"id","id_str","name","screen_name","text","created_at"の部分をツイート別に格納したいです。↓こんな感じに
id | id_str | name | screen_name | tweets | created_at |
---|---|---|---|---|---|
略1 | 略1 | 略1 | 略1 | 略1 | 略1 |
略2 | 略2 | 略2 | 略2 | 略2 | 略2 |
### 発生している問題・エラーメッセージ |
array to string conversionとなり、insertは出来るが、結果が全てarrayになります。
該当のソースコード
twitter.php
1<?php 2$json2 = file_get_contents("tweets.json"); 3$json3 = json_decode($json2, true); 4 5 6if($json3 == NULL){ 7 return; 8}else{ 9 $json_count = count($json3["statuses"]); 10 $screen_name = array(); 11 $name = array(); 12 $tweets = array(); 13 $id = array(); 14 $id_str = array(); 15 $created_at = array(); 16 for($i=$json_count-1; $i>=0; $i--){ 17 $screen_name[] = $json3["statuses"][$i]["user"]["screen_name"]; 18 $name[] = $json3["statuses"][$i]["user"]["name"]; 19 $text[] = $json3["statuses"][$i]["text"]; 20 $id[] = $json3["statuses"][$i]["id"]; 21 $id_str[] = $json3["statuses"][$i]["id_str"]; 22 $created_at[] = $json3["statuses"][$i]["created_at"]; 23 24 25 } 26} 27try{ 28 $sql = new PDO('略',[ 29 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 30 PDO::ATTR_EMULATE_PREPARES => false, 31 ]); 32 }catch(PDOException $e){ 33 echo 'connection error: ' . $e->getMessage(); 34 } 35 36 $params ='INSERT INTO php_json (id, id_str, name, screen_name, tweets, created_at) VALUES (:id, :id_str, :name, :screen_name, :tweets, :created_at)'; 37 $stmt = $sql->prepare($params); 38 $stmt->execute(array(':id'=>$id, ':id_str'=>$id_str, ':name'=>$name, ':screen_name'=>$screen_name, 'tweets'=>$text, 'created_at'=>$created_at));} 39 40?>
補足情報(FW/ツールのバージョンなど)
ライブラリはtwitteroauth、phpはバージョンは7.3.6です。
回答3件
あなたの回答
tips
プレビュー