Skip to content

Instantly share code, notes, and snippets.

@basdenooijer
Created March 30, 2011 07:44
Show Gist options
  • Save basdenooijer/894014 to your computer and use it in GitHub Desktop.
Save basdenooijer/894014 to your computer and use it in GitHub Desktop.
Solarium client adapter ZendHttp examples
<?php
// this example assumes Solarium and Zend_Framework are in your include_path
// make Solarium available
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
// make Zend Framework available
require('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
// create a client instance
$client = new Solarium_Client;
$client->setHost('192.168.1.2');
$client->setPort(8983);
$client->setPath('/solr/');
$client->setCore('geonames');
$client->setAdapter('Solarium_Client_Adapter_ZendHttp');
// you can set zendHttp options as an array
// see the zend_http manual for available options
$client->setAdapterOptions(array('useragent' => 'MySolrClient'));
// you can also access the zend_http instance directly
$zendHttp = $client->getAdapter()->getZendHttp();
// and alter the request, for instance by adding a custom header
$zendHttp->setHeaders(array('x-myheader' => 'myvalue'));
// execute a basic select query and display numFound
$query = new Solarium_Query_Select;
$result = $client->select($query);
echo 'Number of results found: ' . $result->getNumFound();
// accessing raw request and response data (you should see the custom useragent and header in the request)
var_dump($zendHttp->getLastRequest());
var_dump($zendHttp->getLastResponse());
<?php
// this example assumes Solarium and Zend_Framework are in your include_path
// make Solarium available
require('Solarium/Autoloader.php');
Solarium_Autoloader::register();
// make Zend Framework available
require('Zend/Loader/Autoloader.php');
Zend_Loader_Autoloader::getInstance();
// create a client instance
$client = new Solarium_Client;
$client->setHost('192.168.1.2');
$client->setPort(8983);
$client->setPath('/solr/');
$client->setCore('geonames');
// you can create a client adapter instance yourself and supply this instance to the client
$adapter = new Solarium_Client_Adapter_ZendHttp(array('adapteroptions' => array('useragent' => 'MySolrClient')));
$zendHttp = $adapter->getZendHttp();
$zendHttp->setHeaders(array('x-myheader' => 'myvalue'));
$client->setAdapter($adapter);
// execute a basic select query and display numFound
$query = new Solarium_Query_Select;
$result = $client->select($query);
echo 'Number of results found: ' . $result->getNumFound();
// accessing raw request and response data
var_dump($zendHttp->getLastRequest());
var_dump($zendHttp->getLastResponse());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment