How can I get all ElasticSearch indexes without using cURL

AndyB

Well-known member
Hello,

Currently with my ElasticSearch v1.2 add-on located here:

https://xenforo.com/community/resources/elasticsearch.4218/

I have the following code which works very well to get all the ElasticSearch indexes.

PHP:
		$curl = curl_init();
		curl_setopt_array($curl, array(
		CURLOPT_RETURNTRANSFER => 1,
		CURLOPT_URL => 'http://localhost:' . $esPort . '/_stats',
		CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
		));
		
		// get data
		$dataJson = curl_exec($curl);
		curl_close($curl);

		// json decode
		$dataArray = json_decode($dataJson, true);

		// throw error if index missing
		if (empty($dataArray))
		{
			throw new XenForo_Exception(new XenForo_Phrase('elasticsearch_index_missing'), true);
		}

		foreach ($dataArray AS $k => $v)
		{
			if ($k == 'indices')
			{
				foreach ($v AS $k2 => $v2)
				{
					$index = $k2;
					$size = $v2['primaries']['store']['size_in_bytes'];

					// convert to MB
					$base = log($size) / log(1024);
					$size = pow(1024, $base - floor($base));
					$size = floor($size);
					$size = $size . 'MB';

					// create stats array
					$stats[] = array(
						'index' => $index,
						'size' => $size
					);
				}
			}
		}

What I would like to do is replace this code with code that doesn't use cURL.

Any help greatly appreciated.

Thank you.
 
With the help of fellow developers, the code below works perfect to replace the cURL code that I was using.

PHP:
		// use Zend_Http_Client to get the data
		$page = 'http://localhost:' . $esPort . '/_stats';
		$client = new Zend_Http_Client();
		$client->setUri($page);
		$responseBody = $client->request()->getBody();
		$dataJson = utf8_encode($responseBody);

Thank you.
 
Not everybody has their elasticsearch installed on the same server as XenForo. You're forcing a localhost connection which won't always work.
 
Back
Top Bottom