I have a php script that is slow, but must complete in it's entirety. At the end of the script it redirects to a url.
I am thinking of somehow displaying an iframe while the scirpt is loading so visitors don't leave the page. What is the best way of doing this? With the iframe there would be no need to redirect after the completion of the script.
<?php
require_once('config.php');
if(!$_SESSION['init']){
die;
}
unset($_SESSION['init']);
require_once('facebook.php');
$data = loaddata();
$facebook = new Facebook(array(
'appId' => $fbappid,
'secret' => $appsecret,
'cookie' => true,
));
$friendsdate = $facebook->api('/me/friends');
if($friendsdate && $friendsdate['data'] && count($friendsdate['data'])){
$friends = array();
foreach($friendsdate['data'] as $f){
$friends[] = $f['id'];
}
sleep(3);
$session = $facebook->getSession();
$params = array('name' => $data['name'],
'start_time' => $data['start_time'],
'end_time' => $data['end_time'],
'description' => $data['description']);
if($data['source']){
$params['source'] = '#'.realpath('image/'.$data['source']);
$facebook->setFileUploadSupport(true);
}
$result = $facebook->api('/me/events', 'POST', $params);
sleep(4);
$eid = $result['id'];
$params = array(
'access_token' => $facebook-> getAccessToken(),
'eid' =>$eid,
'api_key' => $fbappid,
'uids'=> implode(',', $friends),
'format'=>'json-strings',
'personal_message'=> $data['personal_message']
);
$url = 'https://api.facebook.com/method/events.invite';
$facebook->setFileUploadSupport(false);
$result = $facebook->makeRequest($url, $params);
}
header('Location: '.$data['url']);
The proper way to do what you want is to use ajax.
Main page:
Shows some sort of "working" message in a div.
On load, javascript calls out to the worker page.
On response, the javascript replaces the "working" div with the response from the worker page.
Worker page:
Does your long php work.
Displays whatever you think is appropriate.
Here is a tutorial on AJAX to get you started: http://www.w3schools.com/ajax/default.asp
Yes it'll work, facebook api gets very slow at times, so you can use iframe or ajax.
For iframe - just replace the final header('location:... with javascript redirect
echo "<script>self.parent.location = 'http:// ... ';</script>";
Also you must do session_commit somewhere at the script's top, if you're using php sessions because otherwise the iframe could lock the main script. AJAX... not really necessary.
Related
I have read all the postings related to the title. But I have a very simple question. It will be very helpful if anyone can answer that.
What is me/account page in facebook?
I got many postings here such as
Posting to facebook company page with cron php server side
Everything is explained, but I know this sounds funny, but I stuck in the easiest step.
"When using Graph Explorer you would be required to navigate to /me/accounts end point,"
Please help me.
Please check the code
include 'includes/facebook.php';
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXX";
$page_id = "XXXXXXXXXXXX";
$my_url = "http://XXXXXXXXXXX.com";
$page_access_token = "XXXXXXXXXXXXXXX";
//Create the facebook object
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
//Write to the Page wall
try {
$attachment = array(
'access_token' => $page_access_token,
'message'=> "Hello World"
);
$result = $facebook->api('/639386542780904/feed', 'post', $attachment);
} catch(Exception $e) {
echo "error";
// ...
// mail($to, $subject, $message, $headers);
}
me/accounts is used to get a facebook page acces token, so that your app will post as that page. Elsewhere, your app will post as you on that page.
I am using the JS and PHP SDK and I can login a user successfully using facebook.
However logout doesnt seem to work (I can log a user out of my site but not out from facebook ).
Here is my logout code :
require_once('../libs/facebook/src/facebook.php');
$facebook = new Facebook( array (
'appId' => 'xxxx',
'secret' => 'xxxx',
'cookie' => true
)
);
$fb_key = 'fbsr_'.$facebookConfig['app_id'];
// set_cookie($fb_key, '', '', '', '/', '');
//$facebook->setSession(NULL);
$facebook->destroySession();
$facebook->getLogoutUrl();
$_SESSION = array();
session_unset();
session_destroy();
$files = array();
foreach ($_SESSION as $key => $value) {
$files[] = $key;
unset($_SESSION[$key]);
}
I have tried to use the following below to delete the cookie but I got an error message that setcookie is not a function
$fb_key = 'fbsr_'.$facebookConfig['app_id'];
setcookie($fb_key, '', time()-3600);
$facebook->destroySession();
Any suggestions will be appreciated.
For some reason I can't post a comment on your question, so here's an 'answer'.
Have you looked at this SO post?
Delete Facebook Session After Login with PHP SDK
More people are struggling on this, from what I've read. And nobody seems to know if there's a native function within the SDK. So unsetting it manually might be your best bet.
You need to append a user access token to the logout url.
Example from my app.
if($_GET['destroy']){
$facebook->destroySession();
}
$params = array( 'next' => 'https://apps.facebook.com/anotherfeed/?ref=logout&destroy=true', 'access_token' => $access_token);
$logout=$facebook->getLogoutUrl($params); // $params is optional.
I'm have script below to check whether user is my fan page or not. If yes, they will be redirected to another page. If not, they will be show a like box to like the page before proceed.
The script sometimes work but sometimes don't. I expect this to have issue with header() because it got 500 internal server error. However, i already put ob_start(). Shouldn't that solve the problem?
<?
ob_start();
require_once('../../src/facebook.php');
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => false,
));
//Facebook Authentication part
$user_id = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_likes'
)
);
if (!$user_id) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$likes = $facebook->api("/me/likes/PAGE_ID");
if( !empty($likes['data']) ){
ob_end_clean();
header ('location: xyz.php'); //if is a fan, redirect to xyz.php
exit;
} else {
continue; //if not a fan, show below html code
}
?>
<HTML>
.
.
Please like page first
.
.
</HTML>
$config = array(
'appId' => 'yyyyyyyyyy',
'secret' => 'xxxxxxxxxx',
'cookie' => true,
'domain' => true
);
$facebook_client = new Facebook($config);
//Grab the user's session
$session = $facebook_client->getSession();
/*If session does not exist, the user is not loggedin or hasn't added the app
so redirect them to the authorize page.*/
if(!$session){
$text = "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";
echo $text;
exit;
}
$access_token = $access['access_token'];
$params = array('access_token' => $access_token);
try{
$me = $facebook_client->api('/me',$params);
$feed_params = array();
$feed_params['message'] = "Hello world";
$feed_params['link'] = "http://apps.facebook.com/jagdish/";
$feed_params['name'] = "jag";
$feed_params['caption'] = "Trying to post from application";
$feed_params['description'] = "From jag";
$feed_params['access_token'] = $access_token;
$id = me['id'];
$result = $facebook->api('/me/feed/','post',$feed_params);
}
catch(FacebookApiException $e)
{
error_log($e);
}
This is my code. When i executed it, got an exception saying
com.caucho.quercus.QuercusException: com.caucho.quercus.QuercusErrorException: /base/data/home/apps/fbookworkshop/version1.349114876107177725/index.php:49: Fatal Error: Method call 'api' is not allowed for a null value.
Where am i going wrong?
I guess you are mixing 3 (or more) tutorials together!!
Issues in your code:
The first issue:
$text = "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";
In this line:
Where did you set the $oauth_url?
Also no need for the \n
The second issue (mentioned by #fazo).
The third issue, NO NEED for the access_token all together if there's a valid session! so don't set it or use it in any of your requests as long as you are using /me.
The fourth issue:
$result = $facebook->api('/me/feed/','post',$feed_params);
Here you are using the code from another tutorial, since you are using $facebook var instead of $facebook_client
change
$access_token = $access['access_token'];
to
$access_token = $session['access_token'];
and tell us what happened
UPDATE:
looks like you can't use /me
https://github.com/facebook/php-sdk/issues/closed#issue/294
Ok so this is driving me insane! I've got a script written that takes user submitted product information from a previous page and sends it to the user's facebook business page via the facebook php sdk.
Here's the php code:
$app_id = "1234567890";
$app_secret = "1234567890";
$page_id = "1234567890";
$page_token = "1034921234567890";
$store_name = "MyStore.com";
$store_url = 'http://www.mystore.com';
$cur = '$';
$new_message = "New Product!";
$prod_image = $store_url . "/images/" . $_POST['products_image'];
$price = $products_price;
$prod_url = $store_url . '/index.php?main_page=product_info&cPath=' . $current_category_id . '&products_id=' . $products_id;
$prod_name = $_POST['products_name'][1];
$prod_description = $_POST['products_description'][1];
include_once 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true, ));
$attachment = array(
'access_token' => $page_token,
'message' => $new_message,
'name' => $prod_name,
'link' => $prod_url,
'caption' => 'Price: '.$cur . $price,
'description' => $prod_description,
'picture' => $prod_image
);
Everything sends to facebook fine except on some servers$prod_description aka $_POST['products_description'][1] ends up empty. When a var_dump($_POST['products_description']); is placed on the same line, it comes back with:
array(1) { [1] => string(35) "This is a
test product description." }
So I know the information is there and not being overwritten it's just for whatever reason when sent to facebook (with cURL from the include_once file) it doesn't post.
I should also say that this is a Zen Cart mod that is available for download and I while haven't been able to reproduce the error myself (test on 4 different servers) I have had miltiple users add the var_dump and send me the results.
I guess my question is; Is there something (like a ini setting, cURL option, etc) that may vary from server to server that would cause random indexes to empty when sent via cURL and is there something I can do to fix it?
Thanks in advance.
I think problem is in the way data is passed. You have spaces in the description, that can cause problems...
Try explicitly setting enctype for the form to "application/x-www-form-urlencoded"
<form enctype="application/x-www-form-urlencoded" .... >
You can also try to url_encode the product description before passing it to Facebook.
$prod_description = urlencode($_POST['products_description'][1]);