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:
?
Related
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>';
?>
I've created a delete post button on the front end for a Wordpress site. It deletes the post fine but then tries to reload the post again causing a 404. Is there a way I redirect it after deleting to a specific url? This is my code:
function wp_delete_post_link($link = 'Delete This', $before = '', $after = '') {
global $post;
$link = "<a href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";
echo $before . $link . $after;
}
Then on the template:
<?php wp_delete_post_link('Delete This', '<p>', '</p>'); ?>
I think you are looking for the wp_redirect function.
$location = 'http://domainname.com/pagename/';
$status = '301';
wp_redirect( $location, $status );
exit;
Just place after your successful delete code.
How about running this function right after your echo:
header( 'Location: yournewURL.html' ) ;
Replace 'yournewURL.html' with the page you need to redirect to.
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
I am trying to:
Retrieve a friend list from a user with the friend information:
Location
Education
Name
Profile picture
The problem is the standard permissions given only has friend profile picture and name. I used this to get the other information:
$params = array(
'scope' => 'email, friends_likes, user_about_me, friends_about_me, friends_location, friends_website, friends_work_history, friends_education_history'
);
$loginUrl = $facebook->getLoginUrl($params);
}
So now when I log in on my page, Facebook asks me if I'm willing to give those permissions to the application, so that is a good sign.
The problem is I'm still not able to figure out how to get the information. When I try and do
print_r($friends);
or
print_r($friends["data");
I still just get an array with name and ID.
Why am I not seeing the extra information?
This is not the full code, but a lot of it (most came from the example.php from the main Facebook GitHub account):
<?php
if ($user):
echo "Name: " . $user_profile['name']; ?> <br />
<?php echo "Location: " . $user_profile['location']['name']; ?> <br />
<?php echo "Bio: " . $user_profile['bio']; ?> <br />
<?php
foreach($user_profile['work'] as $work) {
echo "Work: " . $work['employer']['name'];?><br />
<?php echo "Position: " . $work['position']['name'];?><br />
<?php
}
?>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture?type=large">
<h3>Your User Object (/me)</h3>
<pre><?php
print_r($friends);
?></pre>
<?php
echo '<ul>';
foreach ($friends["data"] as $value) {
echo '<li>';
echo '<div class="pic">';
echo '<img src="https://graph.facebook.com/' . $value["id"] . '/picture?type=large"/>';
echo '</div>';
echo '<div class="picName">'.$value["name"].'</div>';
echo '<div class="location">'.$value["location"]["name"];
echo '<div class="bio">'.$value["bio"];
echo '</div>';
echo '</li>';
}
echo '</ul>';
?>
The API call (note I made the $permissions just for testing to be sure I was getting it, and they are showing up).
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$permissions = $facebook->api('/me/permissions');
$friends = $facebook->api('/me/friends');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Your API call to /me/friends is incorrect. You need to do something like:
$friends = $facebook->api('me/friends?fields=birthday,location,about...');
You need to add in the extra fields you require as parameters in the API call. I've added a few in the above example, but you will need to add the remaining ones.
i'm stuck at this facebook application, not knowing how do i go ahead.
for the canvas, the app works quite perfectly. i've two pages, quote.html and quote.php. in quote.html, quote.php [which gets the quotes from the database] is called through ajax which displays a quote randomly for 10 secs.
however, for the wall tab/profile box, i can't seem to know where i'm going wrong. i created a page called 'profile_box.php', call quote.php for the quotes, setFBML. while this works on canvas [sans the timing], it doesn't display anything on the wall tab.
<?php
include 'quote.php';
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ( !$row ) {
echo "Empty";
}
else{
$fb_box = "<p>" . h($row['cArabic']) . "</p>";
$fb_box .= "<p>" . h($row['cQuotes']) . "</p>";
$fb_box .= "<p>" . h($row['vAuthor']) . "</p>";
$fb_box .= "<p>" . h($row['vReference']) . "</p>";
}
try{
$url="http://website/name/quote.html";
$facebook->api_client->fbml_refreshRefUrl($url);
$fbml = "<fb:ref url='$url'/>";
if(isset($_REQUEST['fb_sig_page_id'])){
$page_id = $_REQUEST['fb_sig_page_id'];
$profile_type = $_REQUEST['fb_sig_type'];
$facebook->api_client->profile_setFBML($appapikey, $page_id, "$fbml", NULL, NULL, "$fb_box");
}
else {
$is_tab = isset($_REQUEST['fb_sig_in_profile_tab']);
if( !$is_tab ) $user_id = $facebook->require_login();
$result = $facebook->api_client->profile_setFBML($appapikey, $user_id, "$fbml", NULL, NULL, "$fb_box");
}
}
catch(Exception $ex){
echo 'Caught exception: ', $ex->getMessage(), "\n";
}
echo "<fb:add-section-button section=\"profile\" /><br />";
?>
can anyone please give any pointers?
I hate to be the bearer of bad news, but Profile Boxes and the Boxes Tab are gone.
They are being replaced with Application Tabs.
In other words, you will no longer be able to display custom content on a user's profile. It all has to go into a application-specific tab.