Should I use JQuery instead of PHP for loading external JSON? - php

I've got a webpage that makes several calls to external site APIs. Right now, I do this via PHP, and cache the responses to disk (using cachelite) where I can. Even so, the page seems to load very slowly, and I'm struggling to find a likely cause (the page speed tab in Chrome is not providing any useful clues).
So I wonder if there might be some benefit to having the client process the API calls via JQuery instead, so that at least the initial page load for the user is faster. Is it worth me doing this, or would I be better off trying to optimise the PHP code further?

It depends, if you want to have the result of the JSON indexed by Google, you need to parse it through PHP.
If you want the fastest way and less server load, use Javascript to load the JSON, the JSON will be loaded from an external source and cached by the users browser.
What is the load time of the JSON? Maybe the other server is slow?

PHP is server side scipting.
If you are not doing anything on server but just loading the contents from external Site APIs
I think it would be better if you use jQuery or javascript to the job for you.

I've been thinking about the two answers above and think maybe what I could do is combine both approaches, that is to say, have the json fetched and cached by a server-side php script, but have the user-facing PHP files use jquery to load data from the file.

Related

Call all php scripts via ajax?

The idea is that I call all of my php scripts via ajax so the php scripts aren't visible when a user views the sourcecode.
Is this a good ideal especially regarding security and performance of my website?
The PHP code that you write will never been seen by the end user - the code is executed by the server and returns everything outside of the PHP tags (<?php ... ?>). The PHP code inside the tags is NOT returned to the browser.
As far as using AJAX is concerned this won't help you with security, your AJAX calls are only as secure or unsecure as standard GET or POST requests to the server. What AJAX may help you with is performance since it allows you to send and recieve data in the background. If your application is data-centric then AJAX will be useful to communicate pure data with the server. If you have a standard hierarchical website design then there's little user for AJAX, just use links to move around and forms to send data.

How to implement load balancing in simple REST api?

I have simple REST api written in CakePHP (php on apache). Basically it has just one endpoint, let's say /api/something/?format=json. Calling this endpoint doesn't read anything from DB, but internally it's fetching and parsing some external website and returns parsed data to the user in json format. The problem is that fetching and parsing data from external web page may last quite long and therefore I need some load balancing mechanizm which will distribute api calls among several servers.
I have never done any load balancing so I even don't know where to look for info - I am looking for the simplest solution.
Is it a resource that has to be fetched live? Because you could cache the processed data for a certain amount of time.
If it has to be live, doing it in a distributed way is probably not going to solve your problem. (except when you're getting back a dataset that is very large)
http://en.wikipedia.org/wiki/Load_balancing_(computing)
Its pretty late but I guess This is what you need ! Just get the hardware to do all the good stuff !

Connecting Php with javascript

I am trying to make a plugin that people can place on their site to generate a form. I dont want to use an iframe to display the form but use javascript to generate it.
The problem is how do i connect the javascript and php together. My site is programmed in PHP.
Your getting a liite mixed up, I think.
PHP runs on your server. This is the place where you fetch data from the database and create some form of html-response.
Javascript runs in the browser. It can't directly talk to your database.
iframe is a special html-element: Therfore it is passive and can't do anything like creating a form.
You have two ways:
Create a PHP script which handles everything through plain HTTP-Requests. This is the "old school" way and requires a lot of page-reloading.
Write most of the logic in javascript and let it communicate to PHP/your database through AJAX. In this case. Have a look at jQuery which makes AJAX-requests (and a lot of other things) very easy.
One issue you will be faced with is 'Cross site Scripting' with Javascript / AJAX.
You can read up on it a bit here:
http://snook.ca/archives/javascript/cross_domain_aj
Also, thinking your process through, you will need sufficient javascript code to create a 'widget' on any place, and have a way to communicate BACK to your server (keep in mind PHP only runs local on your machine, so it cannot be used remotely in your javascript).
You will probably need to build a JSON API (google / stack search this if needed).
And enable communication from your JAVASCRIPT to the API (don't think of it as to PHP, even tho php will be your API server side language).
Here is an example of a PHP JSON API (on youtube too):
http://www.youtube.com/watch?v=F5pXxS0y4bg
If you put PHP into JavaScript and someone implements this, PHP will compile on their server. So you just can't. You just need to put the form in your plugin.

PHP and cURL or otherwise. Can it retrieve JavaScript generated data?

I know this is very unlikely, but maybe there is another solution.
What I want to do is retrieve JavaScript generated data from an external website. This isn't for an exploit, but rather a test.
I noticed Google cannot index Ajax generated content.. but could they, and more importantly, how?
I guess that is a better way of putting it.
You certainly can do it on a case-by-case basis. However you wouldn't use a tool like cURL. Instead, you'd use something more sophisticated like a browser or javascript interpreter (Rhino springs to mind) to interpret and run the JS and then retrieve the values from there.

Possible to use Javascript to get data from other sites?

Is it possible for a web page using Javascript to get data from another website? In my case I want to get it for calculations and graphing a chart. But I'm not sure if this is possible or not due to security concerns. If it is considered a no no but there is a work around I would appreciate being told the work around. I don't want to have to gather this information on the server side if possible.
Any and all help is appreciated.
Learn about JSONP format and cross-site requests (http://en.wikipedia.org/wiki/JSON#JSONP).
You may need to use the "PHP-proxy" script at your server side which will get the information from the websites and provide it to yours Javascript.
The only reliable way is to let "your" webserver act as a proxy. In PHP you can use curl() to fire a HTTP request to an external site and then just echo the response.
You can't pull data from another server due to the same origin policy. You can do some tricks to get around it, such as putting the URL in a <script> tag, but in your case it wouldn't work for just parsing HTML.
Use simple_dom_html, to parse your data server side. it is much easier than doing it in JavaScript anyways.
A simple way you might be able to do this is to use an inline iframe. If the web page you are getting the data from has no headers, or you can isolate the data being pulled in (to say an image or SWF), this might work.
cross-domain javascript used to be impossible, using a (php-)proxy was a workaround for that.
jsonp changes this entirely, it allows to request javascript from another server (if it has an API that supports jsonp, a lot of the bigger webplayers like google, twitter, yahoo, ... do), specifying the callback-function in your code that needs to be triggered to act on the response.
the response in javascript will contain:
a call to a callback-function you defined
the actual payload as a javascript-object.
frameworks like jquery offer easy support for jsonp out of the box.
once you have the raw data you could tie into google chart tools to create graphs on the fly and insert them in your webapp.
Also worth considering is support for XMLHttpRequest Access Control which is support in some modern browsers.
If the service provider that you are trying to access via a web page has this set up, it is a very simple call to XMLHttpRequest and you will get access to the resources on that site without the need for JSONP (especially useful for requests that are not GET, i.e. POST, HEAD etc)

Categories