Search code examples
phpunirest

How to change response body type stdObject to associative array by default in unirest for php


iam using unirest lib in php, so every request have response

 $response = Unirest\Request::get(
                        env('JIRA_APP_DOMAIN') . 'search',
                        ['Accept' => 'application/json'],
                        [
                            'jql' => 'project = ' . env("JIRA_PROJECT_KEY") . ' AND issuetype in (' . $steps[$request->step] . ') ORDER BY priority DESC, updated DESC',
                            'maxResults' => 20,
                            'startAt' => 0
                        ]
        );

this $response var have body attribute, but it returns StdObject array by default

$response->body// gives StdObject

$response->body->name; // i dont want to use like this

$response->body['name']; // this is i want to use

This is heavy with slow, there is raw_body attribute but i dont want to convert associative array using json_decode function.

There is any way to change by default body return type..


Solution

  • I Found code in this library. it can change default output StdObject to associative array

    Unirest\Request::jsonOpts(true);