0

I have a dict with some values needed to use a PHP web service. This web service need an array in that dict of values. So I have this on my Python script :

data = {
        'resId'         : res_id,
        'collId'        : 'letterbox_coll',
        'table'         : 'res_attachments',
        'data'          : {
            'title'             : 'Rapprochement note interne',
            'attachment_type'   : Config.cfg[_process]['attachment_type'],
            'coll_id'           : 'letterbox_coll',
            'res_id_master'     : res_id
        },
        'fileFormat'    : Config.cfg[_process]['format'],
    }

When I print data I have this :

{'resId': '655', 'collId': 'letterbox_coll', 'table': 'res_attachments', 'data': {'title': 'Rapprochement note interne', 'attachment_type': 'outgoing_mail_signed', 'coll_id': 'letterbox_coll', 'res_id_master': '655'}, 'fileFormat': 'pdf'}

But here is the return of my PHP webservice :

Array
(
    [resId] => 655
    [collId] => letterbox_coll
    [table] => res_attachments
    [data] => res_id_master
    [fileFormat] => pdf
)

The php WS use Slim framework with HTTP Request and Response from this framework, here is a piece of code :

use Attachment\models\AttachmentModel;
use Convert\controllers\ConvertPdfController;
use Convert\controllers\ConvertThumbnailController;
use Convert\models\AdrModel;
use Docserver\models\DocserverModel;
use Docserver\models\DocserverTypeModel;
use History\controllers\HistoryController;
use Resource\controllers\ResController;
use Respect\Validation\Validator;
use setasign\Fpdi\TcpdfFpdi;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\CoreConfigModel;
use Resource\controllers\StoreController;
use Template\controllers\TemplateController;
use SrcCore\models\DatabaseModel;
use Resource\models\ResModel;

class AttachmentController
{
    public function create(Request $request, Response $response)
    {
        $data = $request->getParams();
        file_put_contents('/var/www/html/test.txt', print_r($request, true));

So my question is, why PHP retrieve the 'data' index like this ? And how I could send a array ?

Thanks in advance

6
  • Where is the array in your dict? Commented May 7, 2019 at 14:40
  • I don't understand your question. $request->getParams() is the method in that framework used to get post parameters. Commented May 7, 2019 at 14:41
  • Oh, you're asking why it says [data] => res_id_master instead of showing the nested array. Commented May 7, 2019 at 14:43
  • 1
    All I can think of is that you're not really showing the result of that call. You overwrite the test.txt file every time you call the webservice. Commented May 7, 2019 at 14:44
  • The fact I overwrite my file with file_put_contents is not the problem. After some looks, the $request var doesn't contains the 'data' array. I didn't have 'title', 'attachment_type' etc before getting the post data. I really didn't understand Commented May 7, 2019 at 14:50

1 Answer 1

2

The print_r() command outputs like that. https://www.php.net/manual/en/function.print-r.php

It looks like you HAVE an array. Should you wish to save the array in JSON format, just do the following

$json = json_encode($someArray);

https://www.php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

12 Comments

Shouldn't it show [data] => Array ( [title] => 'Rapprochement note interne' ...)?
Possibly? I think the op needs to edit the question if that's what he's really asking
I think it's pretty obvious. He's sending a parameter with a nested dictionary, but when he prints the parameter in the PHP script it shows a string there.
I admit it took me a few minutes to notice this discrepancy.
yeah exactly, @Barmar is right. I try to json_encode my data var, but it shows the same, not array in my <code>data</code> index
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.