Slow script...want to display iframe
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开发者_StackOverflow['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.
精彩评论