how to send requests to the php libraries from php scripts

how to send requests to the php libraries from php scripts

rlogrlog Posts: 7Questions: 0Answers: 0
edited January 2013 in Editor
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?

Replies

  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    I'm not too sure about this syntax:

    > 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
  • rlogrlog Posts: 7Questions: 0Answers: 0
    No way :-(

    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.
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    edited January 2013
    One question - why is anything other than 201 (Created) an error? It looks to me like the Editor script is returning 200 (Ok), but you are treating it like an error?

    Allan
  • rlogrlog Posts: 7Questions: 0Answers: 0
    Forgot to mention it. I had already fixed the http status from 201 to 200.

    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
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    > However the requests sent by the Editor to the libraries are not "pure" Json.

    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
  • rlogrlog Posts: 7Questions: 0Answers: 0
    I would prefer not to modify anything on the libraries code so the option number 3 is good for me.

    What about the "not existing field" being ignored and not triggering any error? is it by design?

    A..
  • allanallan Posts: 61,446Questions: 1Answers: 10,054 Site admin
    Yes - the SQL is basically just `WHERE id = {var}` - so if nothing matches, nothing happens and no error is reported.

    Allan
This discussion has been closed.