If for some reasons (e.g. old OS port to use PHP 5.4) and phpinfo() reports "cURL Information 7.15.5", the httpClient will output result string to browser instead of returning to the variable.
For example:
$request = \Drupal::httpClient()->post($form_state->getValue('url'), array('body' => $query));
$result = $request->json();
Above code will work fine if phpinfo() report high curl version.. (I tried with 7.41)
But for my old server where version is 7.15, I have to use below code in order to get it to work
ob_start();
$request = \Drupal::httpClient()->post($form_state->getValue('url'), array('body' => $query));
$result = ob_get_contents();
ob_end_clean();
if (!strlen($result)) {
$result = $request->json();
} else {
$result = json_decode(stripslashes($result),true);
}
So I guess it is something to do with guzzleHttp requirement as stated on http://guzzle.readthedocs.org/en/latest/overview.html,
Requirements
PHP 5.4.0
To use the PHP stream handler, allow_url_fopen must be enabled in your system’s php.ini.
To use the cURL handler, you must have a recent version of cURL >= 7.16.2 compiled with OpenSSL and zlib.