XF 2.1 Guzzle Change Information

I still get the same result. :)
The error or a good result?

This code...
Code:
try
{
   $client = \XF::app()->http()->client();
   $response = $client->get("https://api.developertracker.com/anthem/posts");
}
catch (\GuzzleHttp\Exception\RequestException $e)
{
   if (null !== $e->getResponse())
   {
      $error = 'Tracker Error ' . $e->getResponse()->getStatusCode() . ': ' . $e->getResponse()->getReasonPhrase();
   }
   else
   {
      $error = $e->getMessage();
   }

   return $this->error($error);
}

$yourInfo = \GuzzleHttp\json_decode($response->getBody(), true);
print_r($yourInfo);
die();

Works in XF 2.1 and results in this:
Code:
Array
(
[data] => Array
(
[0] => Array
(
                    [content] => <blockquote><div><b><a href="https://twitter.com/Jody_Houser/status/1068415202561933312/">@Jody_Houser</a></b></div><a href="https://twitter.com/msdanifernandez">@msdanifernandez</a> <a href="https://twitter.com/Shanna_Fisher">@Shanna_Fisher</a> I've only done very basic head shots before, so no idea what I'm comfortable with!</blockquote><a href="https://twitter.com/Jody_Houser">@Jody_Houser</a> <a href="https://twitter.com/msdanifernandez">@msdanifernandez</a> <a href="https://twitter.com/Shanna_Fisher">@Shanna_Fisher</a> Omg I wanna do this !!
                    [id] => aeznorqb
[section] =>
[timestamp] => 1543568112
[topic] => tweeted
[topicUrl] =>
[url] => https://twitter.com/SamMaggs/status/1068428218355478528/
[urlHash] => 87beba067afa946bb957c18196af2e68b46df521
[account] => Array
(
[identifier] => SamMaggs
[service] => Twitter
[developer] => Array
(
[group] => BioWare
[name] => Sam Maggs
                                    [nick] => Sam...

Which is the same as the original code.

Even my revised code with the header setting returns the same result as the original code. An array.
 
Last edited:
Try $response->getBody()->getContents()


Fillip
That returns this:
Code:
{"data":[{"content":"<blockquote><div><b><a href=Quote in an unquoted attribute value. Probable causes: Attributes running together or a URL query string in an unquoted attribute value.">\"https://twitter.com/Jody_Houser/status/1068415202561933312/\">@Jody_Houser</a>....

The original code I post way back returned an array. And so does the last code I posted before this reply.
 
Note if you leave the 'true' out of this code...
$yourInfo = \GuzzleHttp\json_decode($response->getBody(), true);

You'll get an object and not an array. ;)
 
That returns this:
Code:
{"data":[{"content":"<blockquote><div><b><a href=Quote in an unquoted attribute value. Probable causes: Attributes running together or a URL query string in an unquoted attribute value.">\"https://twitter.com/Jody_Houser/status/1068415202561933312/\">@Jody_Houser</a>....

The original code I post way back returned an array. And so does the last code I posted before this reply.
Interesting, I just mentioned it because it seemed like everywhere in XF 2.1 they were using getContents, though on closer inspection it's only used when the format is plain text.

TIL.


Fillip
 
I just started getting this error today:
An exception occurred: [Error] Call to undefined function GuzzleHttp\json_decode() in src/addons...
From:
Code:
$json = \GuzzleHttp\json_decode($response->getBody(), true);
 
I just started getting this error today:

From:
Code:
$json = \GuzzleHttp\json_decode($response->getBody(), true);
What you're using is for XF 2.1 only. And the error is probably coming from XF 2.0.

try this...
Code:
if(\XF::options()->currentVersionId >= '2010031')
{
    $json = \GuzzleHttp\json_decode($response->getBody(), true);
}else{
    $json = $response->json();
}
 
Top Bottom