# PHP Post to MSSQL with ServiceHandler

**URL:** <https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247>\
**Category:** Server-Side Scripting\
**Created:** [February 23, 2016, 2:59am UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247 "2016-02-23T02:59:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Harvey](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mike_harvey/32/572_2.png) [@Mike\_Harvey](https://community.dreamfactory.com/u/Mike_Harvey)\
**Post date:** [February 23, 2016, 2:59am UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/1 "2016-02-23T02:59:48Z")

</div>

In lieu of existing documentation for the PHP classes, I am hoping someone could shed a little more light on this.

PHP - post process. I am trying to post a record to MS SQL DB. When I do this through the api\_docs it works fine. But when I fire this from another post process, I get the error:

```
**Required field 'id_session' can not be NULL.**

```

I am certain the value is in my payload. I am calling the post using the service handler like this:

`$auditPost= DreamFactory\Core\Utility\ServiceHandler::handleRequest('POST','ap-db','_table/Audit',[],$jsonPayload,'JSON','Content-Type: application/json');`

What I am unsure of are the last two arguments, (the format, and the header) as I could not find any examples. I looked at the api\_docs post in the chrome debugger, and this was my best guess. The $jsonPayload is well-formed json that works fine when submitted vi api\_docs so I know the id\_session value is there {  
“resource”: [{  
“id\_session”: “10A4A35E-0D2B-4A27-A178-4A1F82E72FC2”,  
“form\_name”: “Toolbox Inspection”,  
“start\_time”: “2016-02-22 20:49:01”,  
“end\_time”: “2016-02-23 01:49:49”,  
“GenDate”: “2016-02-23 01:49:49”,  
“InstitutionName”: “Eddie’s Garage”,  
“GenBuilding”: “Back Shack”,  
“GenRoom”: “6”,  
“GenAuditor”: “Joey the Tool”,  
“Question\_id”: null,  
“Violation\_Flag”: null,  
“ShowRisk”: false,  
“Violation\_CD”: “CHEM\_MSDS.001”,  
“Violation\_Qty”: null,  
“Violation\_Note”: null  
}]  
}  
}`

Any ideas would be great. TIA

---

<div class="post-metadata">

**Author:** ![Mike\_Harvey](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mike_harvey/32/572_2.png) [@Mike\_Harvey](https://community.dreamfactory.com/u/Mike_Harvey)\
**Post date:** [February 23, 2016, 1:33pm UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/2 "2016-02-23T13:33:02Z")

</div>

Well after looking at the ServiceHandler function I missed that headers needed to be array, so that is fixed (I think). Headers below are the same as passing from api\_docs which works.  
`$headers = array( 'Accept: application/json', 'Content-Type: application/json' );`  
However problem still exists getting the same error:

` **Required field 'id_session' can not be NULL.** `

Which leaves me to the format “JSON” string in the arguments. Is that correct? According the code in [dr github ServiceHandler](https://github.com/dreamfactorysoftware/df-core/blob/master/src/Utility/ServiceHandler.php) if the $payload is json and the $format is undefined it throws an undeclared format exception.

Anyone have any ideas?

---

<div class="post-metadata">

**Author:** ![Mike\_Harvey](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mike_harvey/32/572_2.png) [@Mike\_Harvey](https://community.dreamfactory.com/u/Mike_Harvey)\
**Post date:** [February 23, 2016, 1:41pm UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/3 "2016-02-23T13:41:45Z")

</div>

For clarity, my call now looks like this:

`$headers = array( 'Accept: application/json', 'Content-Type: application/json' ); $auditPost= DreamFactory\Core\Utility\ServiceHandler::handleRequest('POST','ap-db','_table/Audit',[],$jsonPayload,'JSON',$headers);`

The only element from the api\_docs not present is the $query **filters=\*** which I do not want. I only need the id back which IIRC is the default. BTW I can also add records via the Data tab.

---

<div class="post-metadata">

**Author:** ![leehicks](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/leehicks/32/55_2.png) [@leehicks](https://community.dreamfactory.com/u/leehicks)\
**Post date:** [February 23, 2016, 7:11pm UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/4 "2016-02-23T19:11:13Z")

</div>

Try this…(just now getting around to documenting use of platform resource, see wiki)

```auto
$payload = ['resource' => [['name' => 'test', 'complete' => false]]];
$result = $platform['api']->post->__invoke('mysql/_table/todo', $payload);
echo print_r($result);

```

---

<div class="post-metadata">

**Author:** ![Mike\_Harvey](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mike_harvey/32/572_2.png) [@Mike\_Harvey](https://community.dreamfactory.com/u/Mike_Harvey)\
**Post date:** [February 23, 2016, 11:51pm UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/5 "2016-02-23T23:51:20Z")

</div>

Worked like a charm thanks!

---

<div class="post-metadata">

**Author:** ![Mike\_Harvey](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/mike_harvey/32/572_2.png) [@Mike\_Harvey](https://community.dreamfactory.com/u/Mike_Harvey)\
**Post date:** [February 24, 2016, 5:56pm UTC](https://community.dreamfactory.com/t/php-post-to-mssql-with-servicehandler/2247/6 "2016-02-24T17:56:56Z")

</div>

Any example using the platform api post to create a file in a new sub-folder on S3 (or any storage service for that matter)? I’m trying to upload an image to s3 in a new subfolder, and the api docs are not much help.
