I have a few websites that have a custom Facebook feed which I load in using the Graph API.
In the past everything loaded very quick but since about a week or two all sites stop loading at the feed part and only finishes loading completely after 30 seconds or more.
What can be the cause of this?
I am sure it is the Facebook feed because when I remove it the site loads quickly again.
Below is an example feed from a website which has this issue:
$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");
$feedarray = json_decode($json_object);
$f = 0;
foreach ( $feedarray->data as $key => $feed_data )
{
if($feed_data->full_picture != ''){
$fbimage = $feed_data->full_picture;
}else{
$fbimage = 'assets/images/fbnoimg.jpg';
}
$shortstrfb = substr($feed_data->message, 0, 170) . '...';
if($feed_data->message != ''){
$f++;
}
if($f > 4){
break;
}
if($feed_data->message != '' && $feed_data->from->name == 'facebook page name'){
$facebookfeed .= '
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="single-product-wrap fbwrapdiv">
<div class="product-image">
<a href="'.$feed_data->permalink_url.'" target="_blank">
<span class="datefb">'.date("d-m-Y",strtotime($feed_data->updated_time)).'</span>
<img class="fbimgclass" src="'.$fbimage.'" alt=""></a>
<div class="product-action">
<i class="fab fa-facebook-f"></i>
</div>
</div>
<div class="product-content">
<div class="price-box">
<p class="facebooktext">'.$shortstrfb.'</p>
</div>
</div>
</div>
</div>';
}
}
echo $facebookfeed;
I fixed it by using curl.
I replaced:
$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");
$feedarray = json_decode($json_object);
With:
$graph_url = 'https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$feedarray = json_decode($output);
Related
I'm using this code:
<div class="fb-like" data-href="https://web.facebook.com/mykliktransport/" data-layout="box_count" data-action="like" data-size="large" data-show-faces="false" data-share="false"></div>
it is showing like this:
but what is want is only the count to show it here :
Its working now... This may help
Get your token number from https://developers.facebook.com/docs/graph-api
<?
$fb_page = '1907481709532899';
$access_token = 'Token Number';
$url = "https://graph.facebook.com/v2.2/".$fb_page.'?access_token='.$access_token;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
$details = json_decode($result,true);
echo $details['likes'];
?>
I'm back again. Here is the issue. In implementing ReCaptcha 2.0, I've downloaded file from Github and did all that was instructed (well apparently not or I wouldn't have an issue). My form automatically loads with error showing "missing-input-response" right above the submit button. I can, however, fill out and submit the form and it redirects to the "thank you" page. If I try to submit the form without checking the box, it gives the error (which is good) but the error is there no matter what. What do I need to do? I would really appreciate any help.
Here is my code:
<?php
require('recaptcha-master/src/autoload.php');
$siteKey = 'MY SITE KEY';
$secret = 'MY SECRET KEY';
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
$gRecaptchaResponse = $_POST['g-recaptcha-response']; //google captcha post data
$remoteIp = $_SERVER['REMOTE_ADDR']; //to get user's ip
$recaptchaErrors = ''; // blank variable to store error
$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); //method to verify captcha
if ($resp->isSuccess()) {
// send mail or insert in db or do whatever you wish to
$emailbody = 'Name: '.$_POST['name']."\n"
.'Phone: '.$_POST['phone']."\n"
.'Email: '.$_POST['email']."\n"
.'Message: '.$_POST['message'];
mail('newsong80#maxxsouth.net', 'More Information', $emailbody);
echo "<meta http-equiv='refresh' content=\"0; url=thankyou.php\">";
} else {
$recaptchaErrors = $resp->getErrorCodes(); // set the error in varible
}
?>
**Here is my form:**
<form action="contact.php" method="POST" title="Contact us for more information">
<p><b>Name:<br>
</b>
<input name="name" type="text" required id="name" tabindex="1" value="" size="50" maxlength="50"/>
<br/> Phone:
<br>
<input name="phone" type="text" id="phone" tabindex="2" value="" size="50" maxlength="50"/><br/>
<b>E-mail:</b><br>
<input name="email" type="text" id="email" tabindex="3" value="" size="50" maxlength="25"/><br/>
<br>
<b>Message:</b><br/>
<textarea name="message" cols="60" rows="10" maxlength="150" id="message" tabindex="4"></textarea><br>
<br>
<div class="g-recaptcha" data-sitekey="MY SITE KEY"></div>
<br>
<?php
if ( isset( $recaptchaErrors[ 0 ] ) )
echo $recaptchaErrors[ 0 ];
?>
<p> </p>
<p><input name="submit" type="submit" formmethod="POST" onClick="MM_validateForm('name','','R','phone','','NisNum','email','','RisEmail','message','','R');return document.MM_returnValue" value="Submit">
</p>
</form>
I found this which is working.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify?");
curl_setopt($ch, CURLOPT_POST, 1);
$campos=array('secret'=>$secreto,'response'=>$TheResponse);
curl_setopt($ch, CURLOPT_POSTFIELDS,$campos);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_exec = curl_exec($ch);
$respuesta_google = json_decode($ch_exec,true);
var_dump($ch_exec);
var_dump($respuesta_google);
curl_close ($ch);
How to display related post by categories with next post of current post (don't latest post or random post). I'm use code related post for twentytwelve theme. but now, author in wentytwelve_entry_meta() is repeat.
pls help me :
<div id="related_posts">
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach((get_the_category()) as $category) {
$id = $category->cat_ID;
}
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3
";
$my_query = $wpdb->get_results($query);
if( $my_query) {
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
echo '</ul>';
}
}?>
WordPress customary is to use WP_Query class to fetch posts from DB, if you can modify your code to use WP_Query it would be easier.
WP_Query Reference
As you are using Custom Query to load your posts from DB, template tags like the_permalink(), the_title_attribute(), the_title() etc will not work properly that is the reason while theme function twentytwelve_entry_meta() fails.
As per codex reference Displaying Posts Using a Custom Select Query
You should try something like this:
global $wpdb;
$query = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON
($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON
($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $category_ids[0]
AND $wpdb->posts.id < $post->ID
ORDER BY id DESC limit 3";
$my_query = $wpdb->get_results($query);
if($my_query) {
global $post;
echo '<h3>Related Posts</h3><ul>';
foreach($my_query as $key => $post) {
//use setup postdata, it works only for variable named $post.
setup_postdata($post);
//you can safely use template tags now.
?>
<li>
<div class="entry-header">
<div class="header-l">
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<p class="datetime">
<?php twentytwelve_entry_meta(); ?>
</p>
</div>
<?php the_post_thumbnail(); ?>
</div>
<div class="entry-summary">
<?php
$str_content = wp_trim_words($post->post_content);
$str_content = str_replace('[', '<', $str_content);
$str_content = str_replace(']', '>', $str_content);
echo $str_content;
?>
</div>
</li>
<?php
}
}
Other interesting post:
What does setup_postdata ($post ) do?
I have a paypal buy button that looks like this:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="business" value="my#email.com">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="item_name_1" value="item1">
<input type="hidden" name="amount_1" value="3">
<input type="hidden" name="item_name_2" value="item2">
<input type="hidden" name="amount_2" value="5">
<input type="hidden" name="custom" value="Admin|2">
<input type="hidden" name="discount_rate_cart" value="5">
<input type="hidden" name="notify_url" value="paypal-ipn.php">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="return" value="Thank-you-page">
<input type="hidden" name="country" value="SE">
<input type="submit" value="Checkout" id="checkoutButton">
</form>
And a IPN that looks like this:
<?php
session_start();
// STEP 1: read POST data
// Reading POSTed data directly from $_POST causes serialization issues with array data in the POST.
// Instead, read raw POST data from the input stream.
$raw_post_data = file_get_contents('php://input');
$raw_post_array = explode('&', $raw_post_data);
$myPost = array();
foreach ($raw_post_array as $keyval)
{
$keyval = explode ('=', $keyval);
if (count($keyval) == 2)
{
$myPost[$keyval[0]] = urldecode($keyval[1]);
}
}
// read the IPN message sent from PayPal and prepend 'cmd=_notify-validate'
$req = 'cmd=_notify-validate';
if(function_exists('get_magic_quotes_gpc'))
{
$get_magic_quotes_exists = true;
}
foreach ($myPost as $key => $value)
{
if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1)
{
$value = urlencode(stripslashes($value));
}
else
{
$value = urlencode($value);
}
$req .= "&$key=$value";
}
// Step 2: POST IPN data back to PayPal to validate
$ch = curl_init('https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
// In wamp-like environments that do not come bundled with root authority certificates,
// please download 'cacert.pem' from "http://curl.haxx.se/docs/caextract.html" and set
// the directory path of the certificate as shown below:
// curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
if( !($res = curl_exec($ch)) )
{
// error_log("Got " . curl_error($ch) . " when processing IPN data");
curl_close($ch);
exit;
}
curl_close($ch);
if(strcmp($res, "VERIFIED") == 0)
{
// The IPN is verified, process it:
// check whether the payment_status is Completed
// check that txn_id has not been previously processed
// check that receiver_email is your Primary PayPal email
// check that payment_amount/payment_currency are correct
// process the notification
// assign posted variables to local variables
$custom = explode('|', $_POST['custom']);
$user = $custom['0'];
$itemAmount = $custom['1'];
$item_name = "";
for($i = 0 ; $i < $itemAmount ; $i++)
{
$item_name .= $_POST['item_name_' . ($i + 1)] . '<br>';
}
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'] . ' ' . $_POST['mc_currency'];
$txn_id = $_POST['txn_id'];
$receiver_email = $_POST['receiver_email'];
$payer_email = $_POST['payer_email'];
$accstatus = $_POST['payer_status'];
include("../config.php");
$sql = "INSERT INTO `purchase` (`product`,`member`,`amount`,`ppmail`,`accstatus`,`status`)
VALUES
('$item_name','$user','$payment_amount','$payer_email','$accstatus','$payment_status')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$post_data_string = serialize($_POST);
mail('mail#domain.com', 'PayPal IPN', $post_data_string);
mysql_close($con);
// IPN message values depend upon the type of notification sent.
// To loop through the &_POST array and print the NV pairs to the screen:
foreach($_POST as $key => $value)
{
echo $key." = ". $value."<br>";
}
}
else if(strcmp($res, "INVALID") == 0)
{
// IPN invalid, log for manual investigation
echo "The response from IPN was: <b>" .$res ."</b>";
}
?>
Now why does not the custom field get passed on to the API?
I do get this in error logging, because custom[0] = "" and custom[1] does not exist:
HP Notice: Undefined offset: 1 in /home1/planetzi/public_html/firefallcrystite/action/paypal-ipn.php on line 79
<?php
define('YOUR_APP_ID', 'x');
define('YOUR_APP_SECRET', 'x');
function get_facebook_cookie($app_id, $app_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $app_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
$url = 'https://graph.facebook.com/me?access_token=' . $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$user = json_decode($response);
print_r($user);
?>
<html>
<body>
<?php if ($cookie) { ?>
Welcome <?php ?>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
cookie: true, xfbml: true});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload();
});
</script>
</body>
</html>
Does anyone know why I am getting this exception? print_r of $user is yielding
stdClass Object ( [error] => stdClass Object ( [type] => OAuthException [message] => An active access token must be used to query information about the current user. ) )
Why is this happening?
well for one thing $access_token isn't set before it's used?