1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can I get all ElasticSearch indexes without using cURL

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 15, 2015.

  1. AndyB

    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($dataJsontrue);

            
    // 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.
     
    Marcus likes this.
  2. AndyB

    AndyB Well-Known Member

    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.
     
  3. Daniel Hood

    Daniel Hood Well-Known Member

    Not everybody has their elasticsearch installed on the same server as XenForo. You're forcing a localhost connection which won't always work.
     
  4. AndyB

    AndyB Well-Known Member

Share This Page