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...

Dreen
  • 6,128
  • 10
  • 44
  • 67

3 Answers3

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();
searsaw
  • 2,835
  • 18
  • 30
  • its not obvious from the auto-generated documentation because the variable is just called $args http://phphttpclient.com/docs/source-class-Httpful.Handlers.JsonHandler.html#9 – nick fox Oct 15 '14 at 11:51
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);
  • but how can i use this option apart from modifying the librarys source code – Dreen Nov 19 '13 at 20:34
  • 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. – João Paulo de Lima Nov 21 '13 at 16:13
  • I'm also annoyed by same issue, so I filter `$response->body` with a function named `stdclass_to_array`: https://gist.github.com/kaorukobo/9267221#file-stdclass_to_array_with_json_encode_decode_for_httpful-php – kaorukobo Feb 28 '14 at 08:11
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

Piuma
  • 11
  • 2