Facebookの写真と本文を自分のサイトにもってきて、自由にレイアウトしたいです。
以下のサイトのようなイメージです。
http://www.awajishimahighwayoasis.com/
色々方法があるようで、試してみましたがすべてうまくいきません。。
出来れば今、主流で安全な方法をご教授いただければと思います。
試したこと
①AppアクセストークンとfacebookIDをつかってjsで呼び出す
$(function() { $.ajax({ type: "GET", url: "https://graph.facebook.com/FACEBOOKID/feed?fields=permalink_url,message,story,created_time&locale=ja_JP,id&access_token=アクセストークン", dataType: "json", success: function(json) { json = json.data; var num = json.length; var Feed = "<ul>\n"; for (var i = 0; i < num; i++) { var time = parseISO8601(json[i].created_time); var text = json[i].message; var story = json[i].story; var link = json[i].permalink_url; if (!story) { Feed += '<a href="' + link + '" target="_blank"><li><small>' + time + '</small> ' + '<p class="text-overflow">' + text + '</p></li></a>\n'; } else { Feed += '<a href="' + link + '" target="_blank"><li><small>' + time + '</small> ' + '<p class="text-overflow">' + story + '</p></li></a>\n'; } } Feed += '</ul>\n'; $("#facebook").append(Feed); } }); }); function parseISO8601(str) { var date = str.match(/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/); return date = date[1] + "年" + date[2] + "月" + date[3] + "日"; } </script>
https://blog.gurunpa.com/facebook-graphapi-json
エラー:(#100) Tried accessing nonexisting field (feed) on node type (Application)
②AppアクセストークンとfacebookIDを使ってPHPで呼び出す
$url="https://graph.facebook.com/FACEBOOKID/feed?access_token=アクセストークン&limit=30&locale=ja&fields=created_time,message,full_picture,permalink_url"; $jsonData = file_get_contents($url, false, stream_context_create($options)); $json = json_decode($jsonData, true); // jsonデータの整形・出力 foreach ($json[data] as $data) : // var_dump($data); $f_contentStr = $data[message]; $f_contentStr = strip_tags($f_contentStr); // テキスト取得 $f_contentStr = preg_replace("/(\015\012)|(\015)|(\012)/", "", $f_contentStr); // 改行を削除 $len_c = mb_strlen($f_contentStr); // コンテンツ文字数 $len_c_max = 50; // 上限文字数 $extention = '・・・'; // 末尾に付ける文字 if($len_c>$len_c_max) { $f_contentStr = mb_substr($f_contentStr,0,$len_c_max); // 文字抜き出し $f_contentStr = $f_contentStr.$extention; // 末尾に付与 }; $f_permalink = $data[permalink_url]; if (isset($data[full_picture])) { $f_pic = $data[full_picture]; }else{ $f_pic = "/img/bg2.jpg"; // 画像がなかったら代わりの画像を }; ?>
https://sutobu000.hateblo.jp/entry/20180710/1531199685
エラー:Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\facebook.php on line 28
両サイトともコピペしてアプリIDとアクセストークンのみ変更いたしました。
解決方分からなくても、このサイト通りでいけたとかでしたら教えてください!
あなたの回答
tips
プレビュー