John Von Colln
John Von Colln

Reputation: 43

Posting to Dreamfactory API via PHP with JSON and cURL

If you are not familiar, please check out dreamfactory.com - a really neat open source API platform to connect to any database, be it mysql, nosql, etc.

I'm having difficulty wrapping my head around using cURL to post JSON to the API that I've setup using Dreamfactory. It's not the POSTing part, but rather how Dreamfactory requires a session be started with the service before you can make calls. I'm seeking advice on best practices on how to create a session first and then make POST requests.

Here's some basic info on how to connect to a Dreamfactory service and make calls:

To login, you simply post to /rest/user/session - easy enough

curl -i -k -3 -X POST https://dsp-yourdsp.cloud.dreamfactory.com/rest/user/session \
 -H "X-DreamFactory-Application-Name: todojquery" \
 -d '{ "email" : "[email protected]", "password" : "yourpassword" }'

The service will return a session_id, which you then can include as a header to make other calls to the API.

A sample POST request, to post JSON to a demo service

curl -i -k -3 -X POST https://dsp-yourdsp.cloud.dreamfactory.com/rest/db/todo \
  -H "X-DreamFactory-Application-Name: todojquery" \
  -H "X-DreamFactory-Session-Token: bhc7lov8r41h4cbn6pue1r63gbgh7jf6" \
  -d '{ "name" : "curl todo", "complete" : false }'

I've been successful at creating a session using PHP with cURL with this code:

<?php

    $data = array("email" => "[email protected]", "password" => "password");                                                                    
    $data_string = json_encode($data); 

    $ch = curl_init('https://api.website.com/rest/user/session');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'X-DreamFactory-Application-Name: website-api'
    ));                                                                                                                   

    $result = curl_exec($ch);
    $response = json_decode($result);
    curl_close($ch);
    var_dump($response->session_id);



?>

So, with the returned array I can get out the session_id, but my question is, what is the best way to use the session_id and make new calls to the API? What's next? Do I leave the curl connection open and create a function to POST? Is there an all in one feature with cURL that could do create a session and POST in one shot?

Upvotes: 2

Views: 2393

Answers (1)

Lee Hicks
Lee Hicks

Reputation: 271

Yes, you can continue with what you have and add the session as X-DreamFactory-Session-Token with the next call. This works great if you going to be doing multiple calls from that script.

If you are looking to make just one call (or a couple), you can also take advantage of Basic HTTP Authentication support added in version 1.9.

The -u option should work (-u myusername:mypassword) or by passing the Authorization header mentioned in the above link (don't forget the 'Basic ' . base64_encode($username . ':' . $password).

Upvotes: 1

Related Questions