how to send requests to the php libraries from php scripts
how to send requests to the php libraries from php scripts
Hello,
I'm trying to send php based requests to a data source based on the Editor Php Libraries.
Can't understand where my requests fail.
Example:
Datasource Code:
[code]
<?php
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 's3storage' )
->fields(
Field::inst( 'bucket' ),
Field::inst( 'folder' ),
Field::inst( 'name' ),
Field::inst( 'size' ),
Field::inst( 'creation_date' ),
Field::inst( 'region' ),
Field::inst( 'uploaded_by' ),
Field::inst( 'hash' ),
Field::inst( 'download_link' ),
Field::inst( 'thumbnail_link' )
)
->process( $_POST )
->json();
?>
[/code]
Requester code:
[code]
<?php
$riga = array(
"bucket" => "a",
"folder" => "a",
"name" => "a",
"size" => "a",
"region" => "a",
"uploaded_by" => "a",
"hash" => "a",
"download_link" => "a",
"thumbnail_link" => "a",
);
$request = array(
"id" => -1,
"table" => "",
"action" => "create",
"data" => $riga
);
$url = "./json_sources/ds_storages3.php";
$content = json_encode($request);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo $response = json_decode($json_response, true);
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
echo $response;
?>
[/code]
When I run the "requester script" this is the response I get:
[code[
ArrayError: call to URL http://www.rdev.it/dev/build/json_sources/ds_storages3.php failed with status 200, response {"id":-1,"error":"","fieldErrors":[],"data":[],"aaData":[]}, curl_error , curl_errno 0
[/code]
I've also tried to dump the POST request:
[code]
[Wed Jan 30 18:20:52 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): {"id":-1,"table":"s3storage","action":"create","data":{"bucket":"a","folder":"a","name":"a","size":"a","region":"a","uploaded_by":"a","hash":"a","download_link":"a","thumbnail_link":"a"}}
[/code]
I really can not understand the reason that makes the request fail:
- the data format?
- the data contents?
- the http request?
I'm trying to send php based requests to a data source based on the Editor Php Libraries.
Can't understand where my requests fail.
Example:
Datasource Code:
[code]
<?php
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../lib/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 's3storage' )
->fields(
Field::inst( 'bucket' ),
Field::inst( 'folder' ),
Field::inst( 'name' ),
Field::inst( 'size' ),
Field::inst( 'creation_date' ),
Field::inst( 'region' ),
Field::inst( 'uploaded_by' ),
Field::inst( 'hash' ),
Field::inst( 'download_link' ),
Field::inst( 'thumbnail_link' )
)
->process( $_POST )
->json();
?>
[/code]
Requester code:
[code]
<?php
$riga = array(
"bucket" => "a",
"folder" => "a",
"name" => "a",
"size" => "a",
"region" => "a",
"uploaded_by" => "a",
"hash" => "a",
"download_link" => "a",
"thumbnail_link" => "a",
);
$request = array(
"id" => -1,
"table" => "",
"action" => "create",
"data" => $riga
);
$url = "./json_sources/ds_storages3.php";
$content = json_encode($request);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
echo $response = json_decode($json_response, true);
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
echo $response;
?>
[/code]
When I run the "requester script" this is the response I get:
[code[
ArrayError: call to URL http://www.rdev.it/dev/build/json_sources/ds_storages3.php failed with status 200, response {"id":-1,"error":"","fieldErrors":[],"data":[],"aaData":[]}, curl_error , curl_errno 0
[/code]
I've also tried to dump the POST request:
[code]
[Wed Jan 30 18:20:52 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): {"id":-1,"table":"s3storage","action":"create","data":{"bucket":"a","folder":"a","name":"a","size":"a","region":"a","uploaded_by":"a","hash":"a","download_link":"a","thumbnail_link":"a"}}
[/code]
I really can not understand the reason that makes the request fail:
- the data format?
- the data contents?
- the http request?
This discussion has been closed.
Replies
> echo $response = json_decode($json_response, true);
If you try:
[code]
$response = json_decode($json_response, true);
if ( ! $response ) {
die( ... );
}
echo $response;
[/code]
Does that work?
Allan
This is a dump of a successful request started by the Editor:
[code]
[Wed Jan 30 20:47:04 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): POST /dev/build/json_sources/ds_storages3.php HTTP/1.1\r\n
[Wed Jan 30 20:47:04 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n
[Wed Jan 30 20:47:04 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): action=create&table=&id=&data%5Bbucket%5D=cazzocazzo&data%5Bfolder%5D=&data%5Bname%5D=&data%5Bsize%5D=&data%5Bcreation_date%5D=&data%5Bregion%5D=&data%5Buploaded_by%5D=&data%5Bhash%5D=&data%5Bdownload_link%5D=&data%5Bthumbnail_link%5D=
[/code]
This is an unsuccessful request started by my code
[code]
[Wed Jan 30 20:49:05 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): POST /dev/build/json_sources/ds_storages3.php HTTP/1.1\r\n
[Wed Jan 30 20:49:05 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): Content-type: application/json\r\n
[Wed Jan 30 20:49:05 2013] [debug] mod_dumpio.c(74): mod_dumpio: dumpio_in (data-HEAP): {"id":-1,"table":"","action":"create","data":{"bucket":"a","folder":"a","name":"a","size":"a","region":"a","uploaded_by":"a","hash":"a","download_link":"a","thumbnail_link":"a"}}
[/code]
The content/type set by the editor is "application/x-www-form-urlencoded" whereas mine is "application/json". I've tried to change it to "application/x-www-form-urlencoded; charset=UTF-8" with no success.
What I really can not understand is the different format of the POST data.
I'll keep on workin on it.
Allan
As I can see, the php libraries respond to the requests using JSON.
However the requests sent by the Editor to the libraries are not "pure" Json.
They seem to be Javascript arrays composed by mixed elements.
I've not been able to create a "pure Json" request that can be handled by the data source.
This is the "raw" format (Apache dump of the post request) of a Create Request sent by the Editor:
[code]
create&
table=&
id=&
data[bucket]=a&
data[folder]=&
data[name]=&
data[size]=&
data[creation_date]=&
data[region]=&
data[uploaded_by]=&
data[hash]=&
data[download_link]=&
data[thumbnail_link]=
[/code]
This is the raw format (Apache dump of the post request) of a "pure Json" request that matches the code published on the page http://editor.datatables.net/server/
[code]
{
"id":-1,
"table":"",
"action":"create",
"data
{
"bucket":"a",
"folder":"a",
"name":"a",
"size":"a",
"region":"a",
"uploaded_by":"a",
"hash":"a",
"download_link":"a",
"thumbnail_link":"a"
}
}
[/code]
I wrote some "quick and dirty" code that builds up the string of the raw post request and that is ok for my tests.
I've been able to note that any CREATE request trying to access a non existent field do not trigger any error (the field is ignored);
An empty fieldError array has been returned for any successful create request.
Thank you for you help, Allan
No they are not - they are HTTP variables - since it is being submitted by HTTP.
> I wrote some "quick and dirty" code that builds up the string of the raw post request and that is ok for my tests.
I suspect that is one of the three ways of doing it. I can see that you could do one of:
1. Detect that plain JSON was sent rather than HTTP variables and decode that
2. Have a special HTTP parameter ( `_json` ) perhaps which, if found, is decoded and passed into Editor
3. Send HTTP variables
Personally I think I might go for the second option - I've seen that work well in REST APIs before. Its a little bit of a hack, but if you document it in your own code, it should be okay :-)
Allan
What about the "not existing field" being ignored and not triggering any error? is it by design?
A..
Allan