XF 2.0 Guzzle code for XF v2.0

AndyB

Well-known member
Hello,

The following code works for XF v2.1

PHP:
// get response
try
{
	$client = \XF::app()->http()->client();

	$response = $client->request('POST', $url, [
		'body' => $json,
		'headers' => [
			'Content-Type' => 'application/json',
			'Content-Length' => strlen($json),
		]
	]);
}
catch (\GuzzleHttp\Exception\RequestException $e)
{
	\XF::logException($e, false, "Similar threads error: ");
	$continue = false;
}

How would I convert the above code to work in XF v2.0 forums?

Thank you.
 
Have you tried $client->post($url, $params)?

I believe that would be the proper request in XF 2.0 and 2.1. But I could be wrong.
 
Last edited:
Yes this is for my Similar threads add-on. I'm switching from cURL to Guzzle and would like to have it work on XF 2.0 and XF 2.1 forums. Currently it only works with XF 2.1.

Looking at the XF 2.0 code as examples, I'm currently trying this code WITHOUT success:

PHP:
            // get json
            $json = json_encode($dsl);
            
            $url = $host . ':' . $port . '/' . $index . '/xf/_search';

            // get response
            try
            {
                $client = \XF::app()->http()->client();

                $response = $client->createRequest('POST', $url, [
                    'body' => $json,
                    'headers' => [
                        'Accept' => 'application/json',
                    ],
                    'query' => ['pretty' => 'true']
                ]);
            }
            catch (\GuzzleHttp\Exception\RequestException $e)
            {
                \XF::logException($e, false, "Similar threads error: ");
                $continue = false;
            }
            
            $data = $response->getBody();

            print_r($data);

            exit();

I get the following output:

190827
 
I do believe you're creating a "request" and not sending it to get a "response".

Look deeper at the XF 2.0 code.

As usual, I could be wrong. ;)

Code:
                $request = $client->createRequest('POST', $url, [
                    'body' => $json,
                    'headers' => [
                        'Accept' => 'application/json',
                    ],
                    'query' => ['pretty' => 'true']
                ]);

                $response = $client->send($request);
 
Last edited:
Thank you, Snog.

When I try the following code:

PHP:
            // get json
            $json = json_encode($dsl);
           
            $url = $host . ':' . $port . '/' . $index . '/xf/_search';

            // get response
            try
            {
                $client = \XF::app()->http()->client();

                $request = $client->createRequest('POST', $url, [
                    'body' => $json,
                    'headers' => [
                        'Accept' => 'application/json',
                    ],
                    'query' => ['pretty' => 'true']
                ]);
            }
            catch (\GuzzleHttp\Exception\RequestException $e)
            {
                \XF::logException($e, false, "Similar threads error: ");
                $continue = false;
            }
           
            $response = $client->send($request);

            print_r($response);

            exit();

I get the following error:

190830
 
I’m sure I’ll regret asking this, but why are you performing API requests directly to Elasticsearch instead of just using XF’s built in search API which, rather than having to perform HTTP requests yourself, would allow you to just call existing methods to get the information you need? It would even allow you to use MySQL search too (albeit I appreciate similar threads functionality might not be as performant with MySQL). Even if you didn’t use the existing search framework, I’m fairly sure the XFES add-on has a bunch of ready made functions for setting up a connection to ES and returning the results you need.

Stop reinventing the wheel and try and make use of (and comprehend) the very reusable code that already exists!
 
Yes this is for my Similar threads add-on. I'm switching from cURL to Guzzle and would like to have it work on XF 2.0 and XF 2.1 forums. Currently it only works with XF 2.1.

Given that XF2.0 uses Guzzle 5.3 and XF2.1 uses Guzzle 6.x, which are incompatible with each other - you are in for a world of pain trying to support both in the one addon - large parts of the syntax have changed and the responses returned by Guzzle have completely changed in Guzzle 6 due to their implementation of PSR7.

This is one of those examples of an exception to the suggestion that addons should support both XF2.0 and XF2.1 ... if you use Guzzle - you'll either need to add a lot of conditional code (messy!) or instead maintain two different addons.
 
It really depends on what he's doing. I would hardly call a single conditional messy. But again it depends on what is being done with Guzzle and a judgement could be made at that time.

IE \XF::app()->http()->client()->get(...) works in both versions.
 
It really depends on what he's doing. I would hardly call a single conditional messy. But again it depends on what is being done with Guzzle and a judgement could be made at that time.

IE \XF::app()->http()->client()->get(...) works in both versions.

For sure - if you are just making a simple call to an external resource, it's unlikely to be an issue - the XF core does a good job of abstracting the details.

However, if you use an external library which relies on the underlying Guzzle code - then it gets a lot more complicated, since that library will be expecting a specific version of a Guzzle client and the version you pass to it will vary based on the version of XF your addon is installed on.
 
For sure - if you are just making a simple call to an external resource, it's unlikely to be an issue - the XF core does a good job of abstracting the details.

However, if you use an external library which relies on the underlying Guzzle code - then it gets a lot more complicated, since that library will be expecting a specific version of a Guzzle client and the version you pass to it will vary based on the version of XF your addon is installed on.
I don't disagree with you. That's why I say a judgement needs to be made based on what's being done. ;)
 
Back
Top Bottom