4

I'm using HTTPful to send some requests in PHP and get data in JSON, but the library is converting the result into objects, where I want the result to be an array. In other words, its doing a json_decode($data) rather than json_decode($data, true).

There is, somewhere, an option to use the latter, but I can't figure out where. The option was added in v0.2.2:

- FEATURE Add support for parsing JSON responses as associative arrays instead of objects

But I've been reading documentation and even the source, and I don't see the option anywhere... The only way I can think of is making my own MimeHandlerAdapter which does a json_decode($data, true) but it seems like a pretty backwards way of doing it if there is an option somewhere...

1

3 Answers 3

3

It may be a little late to answer this, but I did a little research while using Httpful and found the answer. Httpful uses a default set of handlers for each mime type. If one is registered before you send the request, it will use the one you registered. Conveniently, there is an Httpful\Handlers\JsonHandler class. The constructor takes an array of arguments. The only one it uses is $decode_as_array. Therefore, you can make it return an array like this:

// Create the handler
$json_handler = new Httpful\Handlers\JsonHandler(array('decode_as_array' => true));
// Register it with Httpful
Httpful\Httpful::register('application/json', $json_handler);
// Send the request
$response = Request::get('some-url')->send();

UPDATE

I realized that it sometimes parses the response into a funky array if you don't tell the request to expect JSON. The docs say it's supposed to work automagically, but I was having some issues with it. Therefore, if you get weird output, try explicitly telling the request to expect JSON like so:

$response = Request::get('some/awesome/url')
    ->expects('application/json')
    ->send();
Sign up to request clarification or add additional context in comments.

1 Comment

its not obvious from the auto-generated documentation because the variable is just called $args phphttpclient.com/docs/…
2

I never used this library. But in a research I found that you can find this option at src/Httpful/Handlers/JsonHandler.php on line 11.

There you will see:

private $decode_as_array = false;

And this flag is used at the same file on line 27:

$parsed = json_decode($body, $this->decode_as_array);

3 Comments

but how can i use this option apart from modifying the librarys source code
I don't know how your library exactly works. So, another developer probably can help you better than me. But I think if you change the privilege of $decode_as_array to public, you can manipulate it in the two ways freely.
I'm also annoyed by same issue, so I filter $response->body with a function named stdclass_to_array: gist.github.com/kaorukobo/…
1

You have to set decode_as_array to true value, to do this:

\Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));

before Request::get calling

Comments

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.