public function getResults($query)
    {
        $searchResults = [];

        if ($query == '') {
            return $searchResults;
        }

        if ($this->searchEngineId == '') {
            throw new \Exception('You must specify a searchEngineId');
        }

        $client = new Client();
        $result = $client->get('http://www.google.com/cse', ['query' => ['cx' => $this->searchEngineId,
                'client' => 'google-csbe',
                'num' => 20,
                'output' => 'xml_no_dtd',
                'q' => $query,
            ],
        ]);

        if ($result->getStatusCode() != 200) {
            throw new Exception('Resultcode was not ok: '.$result->getStatusCode());
        }

        $xml = simplexml_load_string($result->getBody());

        if ($xml->ERROR) {
            throw new Exception('XML indicated service error: '.$xml->ERROR);
        }

        if ($xml->RES->R) {
            $i = 0;
            foreach ($xml->RES->R as $item) {
                $searchResults[$i]['name'] = (string) $item->T;
                $searchResults[$i]['url'] = (string) $item->U;
                $searchResults[$i]['snippet'] = (string) $item->S;
                $searchResults[$i]['image'] = $this->getPageMapProperty($item, 'cse_image', 'src');

                $searchResults[$i]['product']['name'] = $this->getPageMapProperty($item, 'product', 'name');
                $searchResults[$i]['product']['brand'] = $this->getPageMapProperty($item, 'product', 'brand');
                $searchResults[$i]['product']['price'] = $this->getPageMapProperty($item, 'product', 'price');
                $searchResults[$i]['product']['image'] = $this->getPageMapProperty($item, 'product', 'image');
                $searchResults[$i]['product']['identifier'] = $this->getPageMapProperty($item, 'product', 'identifier');

                $searchResults[$i]['offer']['price'] = $this->getPageMapProperty($item, 'offer', 'price');
                $searchResults[$i]['offer']['pricecurrency'] = $this->getPageMapProperty($item, 'offer', 'pricecurrency');

                ++$i;
            }
        }

        return $searchResults;
    }
public function getResults($query)
    {
        $searchResults = [];

        if ($query == '') {
            return $searchResults;
        }

        if ($this->searchEngineId == '') {
            throw new \Exception('You must specify a searchEngineId');
        }

        $client = new Client();
        $result = $client->get('http://www.google.com/cse', ['query' => ['cx' => $this->searchEngineId,
                'client' => 'google-csbe',
                'num' => 20,
                'output' => 'xml_no_dtd',
                'q' => $query,
            ],
        ]);

        if ($result->getStatusCode() != 200) {
            throw new Exception('Resultcode was not ok: '.$result->getStatusCode());
        }

        $xml = simplexml_load_string($result->getBody());

        if ($xml->ERROR) {
            throw new Exception('XML indicated service error: '.$xml->ERROR);
        }

        if ($xml->RES->R) {
            $i = 0;
            foreach ($xml->RES->R as $item) {
                $searchResults[$i]['name'] = (string) $item->T;
                $searchResults[$i]['url'] = (string) $item->U;
                $searchResults[$i]['snippet'] = (string) $item->S;
                $searchResults[$i]['image'] = $this->getPageMapProperty($item, 'cse_image', 'src');

                $searchResults[$i]['product']['name'] = $this->getPageMapProperty($item, 'product', 'name');
                $searchResults[$i]['product']['brand'] = $this->getPageMapProperty($item, 'product', 'brand');
                $searchResults[$i]['product']['price'] = $this->getPageMapProperty($item, 'product', 'price');
                $searchResults[$i]['product']['image'] = $this->getPageMapProperty($item, 'product', 'image');
                $searchResults[$i]['product']['identifier'] = $this->getPageMapProperty($item, 'product', 'identifier');

                $searchResults[$i]['offer']['price'] = $this->getPageMapProperty($item, 'offer', 'price');
                $searchResults[$i]['offer']['pricecurrency'] = $this->getPageMapProperty($item, 'offer', 'pricecurrency');

                ++$i;
            }
        }

        return $searchResults;
    }
public function getResults ( $ query ) { $ searchResults = [ ] ; if ( $ query == '' ) { return $ searchResults ; } if ( $ this -> searchEngineId == '' ) { throw new \ Exception ( 'You must specify a searchEngineId' ) ; } $ client = new Client ( ) ; $ result = $ client -> get ( 'http://www.google.com/cse' , [ 'query' => [ 'cx' => $ this -> searchEngineId , 'client' => 'google-csbe' , 'num' => 20 , 'output' => 'xml_no_dtd' , 'q' => $ query , ] , ] ) ; if ( $ result -> getStatusCode ( ) != 200 ) { throw new Exception ( 'Resultcode was not ok: ' . $ result -> getStatusCode ( ) ) ; } $ xml = simplexml_load_string ( $ result -> getBody ( ) ) ; if ( $ xml -> ERROR ) { throw new Exception ( 'XML indicated service error: ' . $ xml -> ERROR ) ; } if ( $ xml -> RES -> R ) { $ i = 0 ; foreach ( $ xml -> RES -> R as $ item ) { $ searchResults [ $ i ] [ 'name' ] = ( string ) $ item -> T ; $ searchResults [ $ i ] [ 'url' ] = ( string ) $ item -> U ; $ searchResults [ $ i ] [ 'snippet' ] = ( string ) $ item -> S ; $ searchResults [ $ i ] [ 'image' ] = $ this -> getPageMapProperty ( $ item , 'cse_image' , 'src' ) ; $ searchResults [ $ i ] [ 'product' ] [ 'name' ] = $ this -> getPageMapProperty ( $ item , 'product' , 'name' ) ; $ searchResults [ $ i ] [ 'product' ] [ 'brand' ] = $ this -> getPageMapProperty ( $ item , 'product' , 'brand' ) ; $ searchResults [ $ i ] [ 'product' ] [ 'price' ] = $ this -> getPageMapProperty ( $ item , 'product' , 'price' ) ; $ searchResults [ $ i ] [ 'product' ] [ 'image' ] = $ this -> getPageMapProperty ( $ item , 'product' , 'image' ) ; $ searchResults [ $ i ] [ 'product' ] [ 'identifier' ] = $ this -> getPageMapProperty ( $ item , 'product' , 'identifier' ) ; $ searchResults [ $ i ] [ 'offer' ] [ 'price' ] = $ this -> getPageMapProperty ( $ item , 'offer' , 'price' ) ; $ searchResults [ $ i ] [ 'offer' ] [ 'pricecurrency' ] = $ this -> getPageMapProperty ( $ item , 'offer' , 'pricecurrency' ) ; ++ $ i ; } } return $ searchResults ; }
Get results from a Google Custom Search Engine.

@param string $query

@return array An associative array of the parsed comment, whose keys are `name`,
`url` and `snippet` and some others

@throws Exception
Get results from a Google Custom Search Engine .