Speed up response time of application that uses web services - php

We have a small application that uses the Facebook API to retrieve the posts of an user that is logged with facebook and translate them using Bing Translate API.
When you first login the server retrieves the first 10 posts, sends them to the Bing Api for translation and then sends them to the user. When the user scrolls to the bottom of the page using AJAX the server retrieves the next 10 posts and sends them to BING for translation and inserts them in the webpage.
The problem is that like this it takes a really long time for posts to load. Is there any way to speed up the process?
Using cache to store the next posts before acctualy being needed and loading them from the cache is an option?
include 'test_translate.php';
function loadFirst($facebook){
$result = $facebook->api('/me/home?fields=from,type,story,message,picture,link,source,name,caption,description&limit=10' , 'GET');
$posts = $result['data'];
parse_str($result['paging']['next']);
?>
class="post_paging">
<?php
$toTranslate = "";
foreach($posts as $post){
$toTranslate.= "<div style=\"background-color:#72b0c9; margin:auto; padding-left:15px; padding-right:15px; padding-top:15px; padding-bottom:15px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;\">
<p><a target=\"_blank\" href = \"http://www.facebook.com/\"";
$toTranslate.= $post['from']['id']."\">".$post['from']['name']."</a> </p>";
if(isset($post['story']))
$toTranslate .= "<p>" . $post['story'] . "</p>";
if(isset($post['message']))
$toTranslate .= "<p>" . $post['message'] . "</p>";
if(isset($post['picture']))
$toTranslate .= '<a target="_blank" href=' . $post['link'] . '><img src="' . $post['picture'] . '"/></a>';
if(isset($post['name']))
$toTranslate .= "<p>" . $post['name'] . "</p>";
if(isset($post['caption']))
$toTranslate .= "<p>" . $post['caption'] . "</p>";
if(isset($post['description']))
$toTranslate .= "<p>" . $post['description'] . "</p>";
$toTranslate.= "</div></br></br>";
}
$result = translate($toTranslate, 'it');
echo $result;
echo "</div>";
}
?>

Related

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>';
?>

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

Insert Image Dialog when loading a document via google drive sdk

I am using the php google drive sdk, and programatically loading a document from Drive into a div. The document loads correctly and I can edit and save without issue. The problem occurs when I attempt to use the insert image dialog via the document menu, it just loads a blank white box.
What do I need to do in order to get the correct insert image dialog to load as if I was using the document directly from within drive? Below is the code I use to load the document:
function loadDocumentForView()
{
$service = buildServiceAccountService("Google Drive account");
$file = $service->files->get($_SESSION['selectedDocument']);
$name = $file->getTitle();
$ext = strtolower(substr(strrchr($name, "."), 1));
if ($ext == 'doc' || $ext == 'docx')
{
if ($_SESSION['editSelectedDocument'] == 1)
{
$path = "https://docs.google.com/document/d/" . $_SESSION['selectedDocument'] . "/edit?usp=sharing&embedded=true";
}
else
{
$path = "https://docs.google.com/a/googleAccount/document/d/" . $_SESSION['selectedDocument'] . "/preview";
}
}
else {
echo "Incorrect Format";
return;
}
$html = "";
if ($_SESSION['editSelectedDocument'] == 1)
{
$html = $html . "<div style='text-align: left; margin-right: auto; margin-left: auto; margin-bottom: 15px; width:95%;'><input driveFile='" . $_SESSION['selectedDocument'] . "' type='button' value='Save' onclick='saveChangesAndDownload(this)' style='margin-right: 5px;' fileName='" . $_SESSION['selectedDocumentName'] ."'><input currentFile='" . $_SESSION['selectedDocument'] . "' type='button' value='Exit' onclick='exitWithoutSaving(this)'>";
$html = $html . "<span id='saveFeedbackSpan' class='serverCallResult'></span></div>";
$html = $html . "<object data='" . $path . "' style='text-align: center; margin-right: auto; margin-left: auto; width:95%; height: 99%;' id='documentViewerObject' doc='" . $_SESSION['selectedDocument'] . "'></object>";
}
echo $html;
}
The problem I think is that the insert image dialog uses a google PickerBuilder that uses Google's domain as the origin and if you look at the javascript console you will see a message like
Invalid 'X-Frame-Options' header encountered when loading 'https://docs.google.com/picker?protocol=gadgets&origin=https://docs.google.…NnhLX2lIv83439cgDPmc%22%7D))&rpctoken=hyerknnixivf&rpcService=bgxw5kefbo32': 'ALLOW-FROM https://docs.google.com' is not a recognized directive. The header will be ignored.
4080223570-picker_modularized_i18n_opc__iw.js:1008 Uncaught Error: Incorrect origin value. Please set it to - (window.location.protocol + '//' + window.location.host) of the top-most page
The problem is that you need to find a way to tell the picker what the origin of the document is like specifiend in the
Picker API. I have the same issue and am still looking for a way to do that as well.

Adding embedded images within mail body phpmailer class

Im trying to embed an image within my message body but it ends up as an attachment
$mailer->Subject = APP_NAME . " - " . $name . " send you and Ad : " . $row['name'];
$mailer->IsHTML(true);
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');
//footer
$footer = "Regards<br/><br/>";
$footer .= '<table style="width: 95%">';
$footer .= '<tr>';
$footer .= '<td>';
$footer .= "<strong><span style='font-size: 15px'>NamDimes Team</span></strong><br/>
NamDimes<br/>
Contact Number: " . APP_CONTACT . "<br/>
Email: " . APP_EMAIL . "<br/>
Website: " . APP_WEBSITE . "<br/>";
$footer .= '</td>';
$footer .= '<td style="text-align:right">';
$footer .= '<img src=\"cid:logoimg\" />';
$footer .= '</td>';
$footer .= '</tr>';
$footer .= '</table>';
$mailer->Body = $body . $footer;
$mailer->AltBody="This is text only alternative body.";
$mailer->AddAttachment('../' . $row['image_path'], $row['name'] . ".jpg");
i have set everything else, including the addresses, the mail gets send out, logo image that I want embed in the body gets attached as an attachment, anyone know why?
Don't use $mailer->AddEmbeddedImage, but directly add
<img src="http://.../images/namDiams.png" /> instead.
The mail length should be lighter... And it works.
EDIT
I don't know if it will help you but there is a little mistake here :
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDimes.png');
Should be
$mailer->AddEmbeddedImage('../images/namDiams.png', 'logoimg', 'namDiames.png');//the last param the second 'a' was missing...
Another topic here
I can confirm that user2189925's answer does work. However, I use the absolute path since the location of the calling script is more likely to change than the location of the image.
e.g.
<img src="C:\folder\images\namDiames.png" />
Faced the same problem, then I decided to replace the following
<img src="img/example.jpg"
with
<img src= "https://mysitename.com/img/example.jpg">
and it worked.
just give path of your image to the mail body eg: (img src="../images/cat.jpeg) it will definately work

showing content in profile box from another file

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.

Categories