Dealing with a large quantity of data with a REST web service - php

I am trying to create a web service which takes a number of arrays (of type double) and returns the mean value of each of these arrays.
To get started I have written a PHP script which retrieves the array using the HTTP GET method and calculates its mean, returning it in the response in JSON format:
<?php
header("Content-Type:application/json");
if(!empty($_GET['array'])) {
//calculate mean of each array and send results
$array = $_GET['array'];
$mean = array_sum($array)/count($array);
respond(200,"OK",$mean);
} else {
//invalid request
respond(400,"Invalid Request",NULL);
}
function respond($status,$message,$data) {
header("HTTP/1.1 $status $message");
$response['status']=$status;
$response['message']=$message;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>
However, I realized very quickly that with a large array (say, 15000 values) my query string used to pass the values will become so large as to be impractical, if it even works (I have read that many systems place a limit of ~2000 characters on the length of a URL). So, I am looking for a different way to pass the data to my service that will not be limited in this way. The arrays will be generated by a client program. I am using the latest XAMPP stack for the server.

You can use POST instead of GET for that. The way to do that deppends on the language of your client. For example, using jQuery:
$.post('http://yourserver.com', yourArray, function (data, status) {
//handle the response of the server
});

For the data you're talking about, you're right, GET is not the appropriate way to send it. Use POST.

I'm assuming you have control over the client program, in which case, change the client to use HTTP Request, with the request content in the body.
If you don't have control over the client program, you are out of luck. If the client sends the request in $_GET, you must receive it that way.

Related

php echo something between 2 functions after an AJAX call

I am looking for a way to get an echo (or anything else) between 2 functions, the first being called via Ajax.
Here is what I am doing :
I upload via Ajax a video to the server where it is temporary stored.
then pass the video to a private function that send it to youtube via the API and after, unset the video on the server.
My question : between the 2 functions, is there a way to get from the PHP script something that can be handled by the client to inform him of the progress ?
I tried :
to send JSon
to update a session checked by JS while the Ajax call
flush() and ob_flush()
but always, data is returned at this end of the 2nd function (when I don't need anything anymore), as if, while the call and all that is after, PHP was unable to say something
I 'd like :
function upload()
{
//uploading
if(ok)
{
//echoing something there (the main goal)
goYoutube();
}
}
private function goYoutube() {
//etc...
}
and what I have is :
function upload()
{
//uploading
if(ok)
{
goYoutube();
}
}
private function goYoutube()
{
//etc...
//echoing something there (the bad bad bad useless thing)
}
It is with CodeIgniter and I don't have the hand on the server.
I don't need code, just an idea (which will be greatly appreciated)
Of course, something like "don't lose your time guy, it can't be done" is ok !
Thanks.
PHP is a server side language and I don't think it'll output something until the script finished executing. If I were you I would try to split those functions in two separate AJAX request.
The first request gives the user the answer that everything is ok (or something like "please wait") and the second request makes the call to the YouTube API.
You could do this by establishing a websocket connection and sending events from the server to the client. I usually do this in nodejs using socket.io. I see there is a similar option for PHP: elephant.io
So the order of things would be:
Establish websockets connection with server (before sending video)
In the clientside, listen for the event you will receive and do whatever update there.
Do the video upload.
In the serverside, send a websockets event where you would place you "echo"
Take a look at it, I really think it will do.

Filling data from one website into another website in php/javascript

I try to filling some data from one website into another website, which both website are not holding in the same server and I have no right to access into the back end of the other website.
For example:
I created a website that will collect the following data from the user
- name
- telephone number
- address
Then I have to pass those data (auto fill-in so that I do not have to manually enter the same data) into the other independent website for user information checking (t0 make sure that the address, telephone and address is the valid data).
Does anyone know how can I do it in php/javascript? Any example or tutorial can show?
I would use JSONP to move data between different domains and use JQuery's getJSON method to make a call to the server. The PHP file should return the data in proper format and the client should be able to read it using JQuery.
Here is a sample:
The server-side PHP code
<?php
header("content-type: application/json");
// Create a generic object.
// Assign it the property 'message' - the feedback message we want the user to see.
$rtnjsonobj->message = "You got an AJAX response via JSONP from another site!";
// Wrap and write a JSON-formatted object with a function call, using the supplied value of parm 'callback' in the URL:
echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';
?>
Get data from client
$(document).ready(function() {
$("#jsonpbtn").click(function() {
var surl = "http://www.otherdomain.com/abovepage.php?callback=?";
$.getJSON(surl, function(rtndata) {
alert(rtndata.message);
});
});
});
What your asking is exactly cross-site scripting (XSS). All modern browsers will prevent you from executing any front-end (JS) script on any url which is not in the original domain.
You can try passing GET values into the page and if the devs built handles into their PHP for that, you might be able to populate the fields. I highly doubt this would work because of the massive security hole it would expose.
I don't know what it is your trying to do at the end of the day, but BE VERY CAREFUL. XSS is an exploit and there's a good chance you could get into trouble for it.

PHP web response

I have a program that will make a web request to a php page (there will not be a browser involved here), and I need to send some data using php back to the program... how do I do this?
Just use echo to echo the thing you want sent back.
<?php
echo "thing that i want to send back";
?>
Have you considered a webservice? It might be overkill. Otherwise.. a POST or GET and echoing the response with PHP..?
What you're describing is basically a website.
You should echo what you want to return, like the others said.
for c# application, you should wait for response, and read the response stream to get the echos.
public GetResponeFromPHP()
{
HttpWebRequest request = null;
HttpWebResponse resp = null;
try
{
request = (HttpWebRequest)WebRequest.Create("http://www.URL_of_php.php");
request.Method = "POST";
request.Credentials = CredentialCache.DefaultCredentials;
// execute the http request and get the response
resp = request.GetResponse() as HttpWebResponse;
if (resp.StatusCode == HttpStatusCode.OK)
{
//read the response stream
String responseText;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
responseText = sr.ReadToEnd();
}
// Now use respones text to get the echos //
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (resp != null)
resp.Close();
}
}
post and get really seem like the solution here. Although I am not very comfortable with the technique because it is prone to errors if parameters in the URL are incorrect or not validated. Maybe consider something other than the PHP? but hey the answer is that it would work like the others said above :)
Depending on the type of data you need returned from PHP, and assuming you have control of both the C# application and the PHP application, you will most likely use something such as the WebClient class, which allows you to make an HTTP call to a URL, which you would point to your PHP application. Once the request is made, you do whatever processing is necessary in your PHP application, and then most likely echo it back to the C# application (also, depends on the type of data you're using)
A simple HTTP response consist of headers and body.
The headers are not displayed in your web browser, but take place in the client(browser) server communication (web server) - simply said.
The web server will return to your program also so named headers, for which you will have to take care about.
Using your php script you can sent your output with echo:
<?php
echo 'Responding to the program...';
?>

Creating jQuery AJAX requests to a PHP function

So far when creating AJAX requests I have been posting to a separate PHP file. Is it possible to create a jQuery AJAX request that calls a PHP function rather than posts to a separate page?
If you could send me any examples or documentation would be great.
I believe there's a fundamental misunderstanding of how the technology works here.
AJAX (Javascript), Flash, or any client-sided technology cannot directly call PHP functions (or other languages running on the server).
This is true for going the other way around as well (eg: PHP can't call JS functions).
Client and server codes reside on different machines, and they communicate through the HTTP protocol (or what have you). HTTP works roughly like this:
Client (eg: browser) sends a REQUEST -> Server processes request and sends a RESPONSE -> Client gets and displays and/or processes the response
You have to see these requests and responses as messages. Messages cannot call functions on a server-side language directly 1, but can furnish enough information for them to do so and get a meaningful message back from the server.
So you could have a handler that processes and dispatches these requests, like so:
// ajax_handler.php
switch ($_POST['action']) {
case 'post_comment':
post_comment($_POST['content']);
break;
case '....':
some_function();
break;
default:
output_error('invalid request');
break;
}
Then just have your client post requests to this centralized handler with the correct parameters. Then the handler decides what functions to call on the server side, and finally it sends a response back to the client.
1 Technically there are remote procedure calls (RPCs), but these can get messy.
AJAX requests call a URL (make a HTTP request), not a file, in most cases the URL is translated by the server to point at a file (or a php script in your case), but everything that happens from the HTTP request to the response that is received is up to you (on your server).
There are many PHP frameworks that map URL's to specific php functions, AJAX is just an asynchronous way to access a URL and receive a response.
Said URL CAN trigger the server to call a specific function and send back a response. But it is up to you to structure your URL's and server side code as such.
If you're asking whether you can call any arbitrary PHP function with AJAX the answer is no*, for obvious security reasons (in addition to the technical reasons). You could make a PHP script that does different things depending on what parameter it's given (for example, execute a single function) if you don't want to create multiple separate files.
*Although you could make a script that would execute any arbitrary PHP command coming from the client, but that would be very, very, very unwise.
Short answer is "no" but the real answer is that you can fake it. NullUserException's answer is good. You create a server that will take the function name and its parameters. Then the server executes the function, and returns the value.
This was done a while back via a protocol called XML-RPC. There was also an effort called JSON-RPC that used some JS techniques.
One things that's cool about JS is that you can do things like this:
var base64_decode = create_remote_call('base64_decode');
function create_remote_call(name) {
return function(x) {
jQuery.getJSON('url/server.php',
{func:name,arg:x},
function(d){return d;});
}
}
A call to base64_decode('sarefdsfsaes') will make a ajax request and return the value.
That code probably won't work because it hasn't been tested, but it's a function that produces a function that will call the server, and then return the value. Handling more than one argument requires more work.
All that said... in my experience, it's usually good to make all network communications explicit instead of disguising it as a regular function.
you may achieve the same result using a bridge, like my phery library http://phery-php-ajax.net you can call PHP functions directly from Javascript and deal with the value. The AJAX is bound to DOM elements, so you can manipulate the calling DOM or just use jQuery from the PHP side. An example would be:
Phery::instance()->set(array(
'phpfunction' => function(){
return PheryResponse::factory()->jquery('body')->addClass('whoops');
}
))->process();
and in the javascript side (or HTML)
phery.remote('phpfunction');
the equivalent to the https://stackoverflow.com/a/7016986/647380 from John Kawakami answer, using phery is:
function base64($data){
return !empty($data['encode']) ? base64_encode($data['content']) : base64_decode($data['content']);
}
Phery::instance()->set(array(
'base64' => 'base64'
))->process();
function base64(content, decode, output){
phery.remote('base64', {'content': content, 'encode': decode ? 1 : 0}, {'type':'text'}).done(output);
}
base64('asdf', false, function(data){
console.log(data); // or assign to some variable
});
since AJAX is asynchronous and you can't just return a value from the AJAX call, you need a callback, but this would suffice.

Retrieving PHP variables from an AJAX form input

I have a form in a PHP sending variables to a PHP file which duly inserts them into a MySQL table.
I currently have a div displaying the response from the PHP (which is anything that is printed by the PHP).
All works fine. The problem is I want to use variables that are created/updated during the PHP MySQL insert process. I.e. not only show what is printed in that PHP file, but USE those variables.
I have seen complicated use of the JSON Encoding to possibly cross this divide, but I'd love to know if that's the simplest approach. And if anyone has any good links or examples on the subject.
I assume that you want to be able to have multiple pieces of data sent back via AJAX to your page and manipulate those.
JSON is indeed the simplest way to do this. If you use PHP5, you can use json_encode() from the PHP side to send a complicated data type (such as an object or an array) back to the browser page. Then in the javascript, you use eval() on the data that is sent back (ex: var data = eval(response);) to parse it back into a usable complicated type in javascript.
There are tons of tutorials out there that will show you how to do this and explain it in further detail than a response here ever could.
Use PrototypeJS and do it like this:
Have some PHP like this
$jsonHeader = array();
if($_REQUEST['param1'])
{
echo '<p>You passed ' . $_REQUEST['param1'] . '</p>';
$jsonHeader['status'] = 'Success';
}else
{
$jsonHeader['status'] = 'Failed because the request was invalid';
}
if(is_array($jsonHeader) and sizeof($jsonHeader) > 0)
{
header('X-JSON: (' . json_encode($jsonHeader) . ')');
}
Then make your Ajax call like this
new Ajax.Request('dostuff.php', {
method: 'get',
parameters: {'param1': 'this is param 1'},
onSuccess: function(response, jsonHeader){
if(jsonHeader['status'] == 'Success'){
//Everything is OK, do stuff
}else{
alert(jsonHeader['status']);
}
},
onFailure: function(){
alert('Fail!');
}
});
Prototype grabs the X-JSON header returned by PHP and automatically sets the jsonHeader argument of the onSuccess function to a Javascript array of the values that were originally set in PHP.
The above scenario is good as long as the amount of data you're returning to Javascript fits in the HTTP header.
If you need to pass back lots of data, just have PHP output the JSON encoded result rather than making it part of the header. Then you can use the evalJSON() method of the response object in your Ajax call.
You do not have to just show what's 'printed in that PHP file', your PHP file could print JavaScript commends back to your page. You could then, upon receiving the response, execute those commands. I like to use the eval function for this, but many people here will discourage you from doing so :)
Just use the "echo" function to put put PHP variables to the standard output put.
echo $myVarName;
Or, I prefer the printf(), be sure to check for HTML in the input BEFORE you output to avoid XSS issues.
Use something like this:
printf("Your input was: %s", strip_tags(%myInputVar));
Also, remember to use the %d or %f formatters when outputting number for best security.

Categories