# How to convert this update script to PHP?

**URL:** <https://discuss.elastic.co/t/how-to-convert-this-update-script-to-php/263628>\
**Category:** Elasticsearch\
**Created:** [February 8, 2021, 3:48pm UTC](https://discuss.elastic.co/t/how-to-convert-this-update-script-to-php/263628 "2021-02-08T15:48:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![OB1290](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ob1290/32/83316_2.png) [@OB1290](https://discuss.elastic.co/u/OB1290)\
**Post date:** [February 8, 2021, 3:48pm UTC](https://discuss.elastic.co/t/how-to-convert-this-update-script-to-php/263628/1 "2021-02-08T15:48:15Z")

</div>

I'm having trouble converting this request to php :

```auto
POST candidates/_update/a23OcncBXBMGOH6pwXge
{
 "script": {
    "source": """
      if (ctx._source.containsKey('file')) {
        ctx._source.file.add(['filename':params.filename]); // adding a hash map -- could be any JSON primitive
      } else {
        ctx._source.file = [['filename':params.filename]]; // 1-member array
      }
    """,
    "params": {
      "filename": "abc"
    }
  } 
}

```

Here is my attempt :

```auto
$params = [

            'index' => 'candidates',

            'type' => '_doc',

            'id' => $file['candidate_id'],  

        ];

        $data = [

            'script' => [

                'source' => ("ctx._source.containsKey('file')" ? "ctx._source.file.add(params.data)" : "ctx._source.file = [params.data]"),

                'params' => [

                    'data' => $file

                ]

            ]

        ];

        $params['body'] = 

            $data

        ;

        $response = $client->update($params);

```

But it gives me this error :

 ![elasticsearch error](https://us1.discourse-cdn.com/elastic/original/3X/3/1/31ca6b8bf26a9cb5cafa83e90b38464c8c45b870.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 8, 2021, 3:49pm UTC](https://discuss.elastic.co/t/how-to-convert-this-update-script-to-php/263628/2 "2021-03-08T15:49:11Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
