How to display JSON Object Data info? - php

I have this PHP:
<?php
$page_id = 'FACEBOOKPAGEHERE';
$access_token = 'SECRETACCESTOKENHERE';
//Get the JSON
$json_object = #file_get_contents('https://graph.facebook.com/' . $page_id .
'/posts?access_token=' . $access_token);
//Interpret data
$fbdata = json_decode($json_object);
foreach ($fbdata->data as $post )
{
$posts .= '<p>' . $post->message . '</p>';
$posts .= '<p>' . $post->created_time . '</p>';
$posts .= '<p>' . $post->likes . '</p>';
$posts .= '<br />';
}
?>
This code is to retrieve some info of my facebook page, so I can put it in the news box of my webpage.
Facebook give me a JSON file, which is the one I called in the $json_object but when I save this code inside my page test.php, I only see what it is in the picture: http://i.stack.imgur.com/U6WLL.png
I dont know what to do, I barely know PHP and this is new thing for me.
my objective is bring that info to me, pass it through CSS and make it beautiful.
This is the page that I'm making: http://watervfire.tk

Related

php variable using mongodb not showing on website server

i am making a twitter clone website using php, and this is the script for creating a tweet:
<?php
session_start();
require_once('twitterdbconnect.php');
if(!isset($_POST['body'])) { //see if the post variable has the body of our tweet
exit;
}
$user_id = $_SESSION['user']; //if there is a tweet get the users id from session
$userData = $db->users->findOne( array('_id'=>$user_id)); //get the users data by asking the database for this user
$body = substr($_POST['body'], 140); //body is the first 140 characters of the post
$date = date('Y-m-d H:i:s');
$db->tweets->insertOne( array( //create a tweets collection and send the tweet there
'authorId' => $user_id,
'authorName' => $userData['username'],
'body' => $body,
'created' => $date
));
header('Location: home.php');
?>
this is the script of printing it on the home page:
<div>
<?php
$recent_tweets = get_recent_tweets($db);
foreach($recent_tweets as $tweet) {
echo '<p>' . $tweet['authorName'] .'</p>';
echo '<p>' . $tweet['body'] . '</p>';
echo '<p>' . $tweet['created'] . '</p>';
echo '<hr>';
}
?>
</div>
the other variables seem to work, but the body variable does not show up and the end result is so:
How can I make it appear as so:
?

Pulling post from one wordpress site to another with links to each posts

I was trying to pull posts from one of my wordpress site to another using the WP REST API, I have successfully done that and the posts are displaying pretty well on the other site, but the issue now is that I want each post to be clickable, such that it opens the full article (post) when its been clicked on....
$json = file_get_contents('http://mywebsite.com/blog/wp-json/posts?filter[posts_per_page]=4');
// Convert the JSON to an array of posts
$posts = json_decode($json);
foreach ($posts as $p){
echo '<div style="color: #fff; float: left;" class="col-md-3 col-sm-3 col-lg-3">';
// Output the featured image (if there is one)
echo $p->featured_image ? '<img src="' . $p->featured_image->guid . '">' : '';
echo '<h5>Title: ' . $p->title . '</h5>';
// echo '<p>Date: ' . date('F jS', strtotime($p->date)) . '</p>';
$summary = $p->excerpt;
$pos=strpos($summary, ' ', 100);
$summary = substr($summary, 0, 100);
echo '<p>';
echo $summary;
echo '</p>';
echo '</div>';
}
So, what i need right now is to pull the link to each posts alongside....
I am just thinking of something like this: echo '<p>Link: ' . $p->link. '</p>';
You should have $p->link or $p->guid available.
You should be able to do print_r($p); within foreach and see exactly what data you have- would be useful if you'd copy that response here as well as that might help.
Try this it will work for you :
<?php
$link = get_permalink($p->ID); // This will get the link of the post from post ID.
echo '<p>Link: ' . $link. '</p>';
?>

How to Get Posts From Custom post type with WP Rest Api v1.2.5

I want to get the posts from post type = events but it is not showing the correct posts from events post type but from the actual WP-POST
<?php
// Get the JSON
$json = file_get_contents('http://coralgableschamber.org/wp-json/posts?filter[posts_per_page]=1&filter[post_type]=events');
// Convert the JSON to an array of posts
$posts = json_decode($json);
foreach ($posts as $p) {
echo '<p>Title: ' . $p->title . '</p>';
echo '<p>Date: ' . date('F jS', strtotime($p->date)) . '</p>';
// Output the featured image (if there is one)
echo $p->featured_image ? '<amp-img src="' . $p->featured_image->guid . '" width="150" height="110"></amp-img>' : '';
echo '<p>Content: ' . $p->content. '</p>';
}
The event post type have the following posts in it but it is not showing these
It is showing wrong post check this link: http://mxcounters.com/coralgables/AMP/front.html
What is wrong and how i can fix to make it work.
Thanks!
Try using http://coralgableschamber.org/wp-json/posts?type=events instead.
You can look at http://coralgableschamber.org/wp-json/ to see all available paths.

Website takes a long time to load due to steam's inventory api request, how do I fix this?

I have a small issue, I'm currently trying to display a user's inventory using steam's API, everything that I wanted is done. (it gets the players items, name and displays the skin's image) however it takes a good 9-30seconds for the webpage to load, is it possible to make the loading of the data faster?
<?php
if(!isset($_SESSION["steamid"])): ?>
<a> Log In so you can see your inventory! </a>
<?php else: ?>
<div class="GetInventory">
<?php
$player = file_get_contents("http://steamcommunity.com/profiles/" . $_SESSION["steamid"] . "/inventory/json/730/2");
$player = json_decode($player, true);
foreach($player["rgDescriptions"] as $item) {
$result = #file_get_contents("http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=" . str_replace(" ", "%20", $item["market_hash_name"]));
$result = json_decode($result, true);
if(!empty($result["success"]) && $result["success"] == true && !empty($result["median_price"]))
echo '<img id= "skinimg" src="http://steamcommunity-a.akamaihd.net/economy/image/' . $item["icon_url"] . '"> ' . $item["name"] . " - " . $result["median_price"] . "<br>";}
?>
<?php endif; ?>
This is the code I currently have,does anyone have any sugestion?
http://i.imgur.com/rRP3Ne0.png

Need Proper Syntax for Condition (wrap Image with link only when there is one)

we had a developer code something for a Custom Post Type in WordPress that we need to tweak just a touch. They're not available right now, but I think it's a pretty simple PHP issue if knowing the proper syntax. Unfortunately, I'm not completely PHP fluent, so hoping to get some help.
We have a posts landing page where we are just displaying a post thumbnail if there is one, and a link to the full post page (single) ONLY if an email address has been entered into the custom post type. If no email address was entered for the post, no link to the final post. Here's the main chunk of code that is currently working properly for this:
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. get_the_post_thumbnail(get_the_ID(), 'medium')
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. '<a class="linkIcon" href="' . get_permalink() . '#member-top">'
. 'Email & Bio »'
. '</a></p>';
}
$html .= '</div>';
}
All we need to do is tweak so that IF an email address has been entered, the same hyperlink is added around the thumbnail image so it can link to the final post along with the 'Email & Bio' text link. But if NO email address is found, the thumbnail image displays as-is (e.g, no link added).
THANKS!
This would add a link to the thumbnail as well, if the $member_email is not empty.
$team_query = new WP_Query($args);
while ($team_query->have_posts()) {
$team_query->the_post();
$member_email = get_post_meta(
get_the_ID(), '_base_team_email', true
);
// Create anchor tag to use when member email is not empty
$memberEmailAnchorStart = '';
$memberEmailAnchorEnd = '';
if (!empty($member_email)) {
$memberEmailAnchorStart =
'<a class="linkIcon" href="' . get_permalink() . '#member-top">';
$memberEmailAnchorEnd = '</a>';
}
$html .= '<div class="span-6 team-member">';
if (has_post_thumbnail()) {
$html .= '<div class="member-photo-wrap">'
. $memberEmailAnchorStart
. get_the_post_thumbnail(get_the_ID(), 'medium')
. $memberEmailAnchorEnd
. '</div>';
}
if (!empty($member_email)) {
$html .= '<p class="member-email">'
. $memberEmailAnchorStart
. 'Email & Bio »'
. $memberEmailAnchorEnd
. '</p>';
}
$html .= '</div>';
}
Depending on what the linkIcon class does, you might want to remove it from the thumbnail anchor tag.

Categories