Apache Solr with Php - php

I am trying to setup apache solr search for one of my project. I have installed solr 3.6 in my development server and it is reachable with
http://127.0.0.1:8080/solr/admin/
I tried to put the sample application available in php manual but it is setting username and password for solr. I am not sure where can I get this information from. I have also tried below code from net but I was getting 500 error whenever I run it
$options = array (
'hostname' => '127.0.0.1',
);
//$client = new SolrClient($options, "4.0"); // use 4.0 for any version of Solr 4.x, ignore this parameter for previous versions
$doc = new SolrInputDocument();
$doc->addField('id', 100);
$doc->addField('title', 'Hello Wolrd');
$doc->addField('description', 'Example Document');
$doc->addField('cat', 'Foo');
$doc->addField('cat', 'Bar');
$response = $client->addDocument($doc);
$client->commit();
/* ------------------------------- */
$query = new SolrQuery();
$query->setQuery('hello');
$query->addField('id')
->addField('title')
->addField('description')
->addField('cat');
$queryResponse = $client->query($query);
$response = $queryResponse->getResponse();
print_r( $response->response->docs );
Please help

your solr version is 3.6 so you should assign $client = new SolrClient($options);
$client = new SolrClient($options, "4.0"); // I see that in your code you commented this line which is required thinking that it is only for solt 4.x in your case, you should uncomment it and only remove the "4.0" in order to create a client.
To create a client :
$options = array (
'host' => "localhost",
'port' => 8983, //port is required
'path' => '/solr/collection1', //collection1 or core are mandatory it can be just /solr/
);
$client = new SolrClient($options); //for solr 3.x

According to the example code, you are not setting the port in your $options array. Should be:
var $options = array(
'hostname' => '127.0.0.1',
'port' => '8080',
);
That could be the cause of the 500 error.

you need to add 3 things host, port, and webapp. I hope you have included the service.php file in that.
var $options = array(
'hostname' => '127.0.0.1',
'port' => '8080', //Port number where solr runs.
'webapp' => '/solr/', //path of the webapp.
);

Master site configuration
For settings.php file in your master site you do not have to change much. Leave your $databases array as it as. Master site will store all the user names, passwords, and sessions.
For settings.php file in your master site you do not have to change much. Leave your $databases array as it as. Master site will store all the user names, passwords, and sessions.
$databases = array(
'default' =>
array(
'default' =>
array(
'database' => 'drupal1',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
Slave sites configuration
The slave sites shall be connected to the Master site's database for certain tables, specially the ones that include user information. For settings.php file in your slave sites you need to specify master site database and call its users and other tables. We can do so by adding configuration settings within the "prefix" key of the $databases array.
$databases = array(
'default' =>
array(
'default' =>
array(
'database' => 'drupal2',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => array(
'default' => 'drupal2.',
'users' => 'drupal1.',
'sessions' => 'drupal1.',
'role' => 'drupal1.',
'authmap' => 'drupal1.',
'users_roles' => 'drupal1.',
),
),
),
);
For more detailed instruction visit - How to set up and use Apache Solr in php

Related

Error code from API call from one domain but not other Undefined index:

Sometimes my domain (example.test.org) is showing me an error, when making any API call.
"{"error":{"code":500,"message":"Undefined index: DB_HOST","file":"\/var\/www\/app\/config\/production\/database.php","line":7}}".
But with the public IP of my local machine it's working. Any reason it would return an error from one domain but not another? The API is running on Laravel 4.2.
The output of database.php is
<?php
return array(
'default' => 'pgsql',
'connections' => array(
'pgsql' => array(
'host' => $_ENV['DB_HOST'],
'port' => $_ENV['DB_PORT'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASS'],
),
),
);
These values come from /var/www/.env.php which looks like
return array(
'DB_HOST' => 'my-app.cvrrctfasmvk.us-east-1.rds.amazonaws.com',
'DB_PORT' => '*****',
'DB_NAME' => '**************',
'DB_USER' => '**********',
'DB_PASS' => '***********',
'SMTP_HOST' => '*******************',
'SMTP_USER' => '***********************',
'SMTP_PASS' => '********************************',
'AWS_KEY' => '****************************',
'AWS_SECRET' => '*******************',
'AWS_QUEUE' => '*****************************************',
'FB_APP_ID' => '*****************',
'FB_APP_SECRET' => '*********************'
);
DB Host file looks like this by the way. with of course the identifiable values being changed to x
<?php
return array(
'DB_HOST' => 'my-app-.xxxxxx.us-east-1.xxx.amazonaws.com',
'DB_PORT' => 'xxxx',
'DB_NAME' => 'xxxx_app_xxx_db',
'DB_USER' => 'xxxx',
'DB_PASS' => 'xxxx',
'SMTP_HOST' => 'email-xxx.xxxx.amazonaws.com',
'SMTP_USER' => 'xxxxxxx',
'SMTP_PASS' => 'xxxx',
'AWS_KEY' => 'xxx',
'AWS_SECRET' => 'xxxx',
'AWS_QUEUE' => 'https://sqs.xxxxx.amazonaws.com/xxxx',
'FB_APP_ID' => 'xxxxx',
'FB_APP_SECRET' => 'xxxx'
);
It looks like it is having trouble reading the /var/www/.env.php file. As the first item in the array is returning an error.
Your best shot is to check your variables_order string at you php.ini file, it does control the order of the super globals loading and which variables you want to have, most probably you're missing an E in the variables_order string.
From the PHP man pages :
variables_order string
Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set.
Read more here PHP man pages
I think you're loading $_ENV only locally, since you probably have different php.ini files.
Clearly, when you get this error message, it's because your $_ENV variable doesn't have a 'DB_HOST' index.
Sometimes environment variables are not present in the superglobal $_ENV but can be fetched with getenv()
I've just tested this on my webserver (running PHP 5.5.9) :
<?php
// test.php
echo $_ENV['PATH'];
?>
then
$ php test.php
PHP Notice: Undefined index: PATH in /tmp/env.php on line 2
while this works :
<?php
// test.php
echo getenv("PATH");
?>
$ php test.php
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

TYPO3 own file for database connection

I tried to take out my database connection from the LocalConfiguration. But it doesn't work on this way. Do you have any ideas how i can realize it. Here what i tried to make it work:
LocalConfiguration.php:
<?php
include_once 'databaseConn.php';
return [
'BE' => [
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
],
and in the databaseConn.php:
<?php
$TYPO3_CONF_VARS['DB']['database'] = 'db_name';
$TYPO3_CONF_VARS['DB']['host'] = 'localhost';
$TYPO3_CONF_VARS['DB']['password'] = 'password';
$TYPO3_CONF_VARS['DB']['socket'] = '';
$TYPO3_CONF_VARS['DB']['username'] = 'usr_name';
Hope you can help me.
thanks
Chris
Create a file called AdditionalConfiguration.php in same directory. You can override every value there by addressing it directly
$GLOBALS['TYPO3_CONF_VARS']['DB']['database'] = 'custom';
You can also check the ApplicationContext by $context = GeneralUtility::getApplicationContext()->__toString(); which can be set in a .htaccess or vhost config
Use the following code in AdditionalConfiguration.php:
$configurationSettings = array();
#include_once(__DIR__.'/DatabaseCredentials.php');
#include_once(… some other files …);
if (is_array($configurationSettings)) {
foreach ($configurationSettings as $path => $value) {
$GLOBALS['TYPO3_CONF_VARS'] = \TYPO3\CMS\Core\Utility\ArrayUtility::setValueByPath($GLOBALS['TYPO3_CONF_VARS'], $path, $value);
}
}
unset($configurationSettings);
then set your database credentials in DatabaseCredentials.php:
$configurationSettings = array_merge($configurationSettings, array(
'DB/database' => 'local_database',
'DB/username' => 'local_username',
'DB/password' => 'secret'
));
and you're done.
It is better that you add your database connection code into "LocalConfiguration.php".
return array(
'BE' => array(
'debug' => false,
'explicitADmode' => 'explicitAllow',
'installToolPassword' => '$P$CcKE/MYkjKWDzNWsnVZhMBDAttVVrf.',
'loginSecurityLevel' => 'rsa',
),
'DB' => array(
'database' => 'db_name',
'extTablesDefinitionScript' => 'extTables.php',
'host' => 'localhost',
'password' => 'password',
'socket' => '',
'username' => 'username',
),

Laravel SSH to server not stored in config file

I have a project that involves server management and I need to execute some SSH commands.
In Laravel I have the SSH utility (remote), but I have to put the configuration in a file.
I need to connect with the credentials stored in a model from the database.
Any ideas how can I do this?
Something like this:
$connArray = array(
"server" => "8.8.8.8",
"port" => "22",
"user" => "root",
"pass" => "123456"
);
SSH::into($connArray)->run(array(
'cd /var/www',
'git pull origin master',
));
You set edit configuration at runtime:
Create a new connection
(You can safely omit this part, Laravel will create the configuration entry automatically for you, but you might need to create it just for your developers to remember that some configurations are being set during runtime).
'connections' => array(
'runtime' => array(
'host' => '',
'username' => '',
'password' => '',
'key' => '',
'keyphrase' => '',
'root' => '/var/www',
),
),
Set them and do whatever you need:
Config::set('remote.connections.runtime.server', '8.8.8.8');
Config::set('remote.connections.runtime.port', '22');
Config::set('remote.connections.runtime.user', 'root');
Config::set('remote.connections.runtime.pass', '123456');
SSH::into('runtime')->run(array(
'cd /var/www',
'git pull origin master',
));

Zend Framework 2 phpcloud database

I am taking the Zend Framework 2 Skeleton tutorial but have elected to use the mysql database in my container at my.phpcloud.com.
In the global.php file I have:
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mycontainer;host=mycontainer-db.my.phpcloud.com',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
In my local.php file I have:
return array(
'db' => array(
'username' => 'mycontainer',
'password' => 'mypassword',
),
);
I receive no errors, even if I purposely put in incorrect credentials. My view loads properly...except that there are no records. I've verified that the table exists and it is named correctly. I've verified that the table contains records.
If I return the Controller to call an array for the view, rather than a populated ViewModel, it loads the view with errors, as expected, since the AlbumTable does not load. In other words, the View is being routed correctly. It just seems like the database is returning no results. I've copied and pasted everything to the correct files and triple checked it all. I completed this tutorial locally with Zend Server and MySql and it runs perfectly. It's just the PHPCloud, which issues no errors, that seems to be returning no records from its database, whether I purposely introduce bad credentials or not.
Thanks for any assistance in advance.
Try something like this:
$dsn = sprintf(
'mysql:dbname=%s;host=%s',
get_cfg_var('zend_developer_cloud.db.name'),
get_cfg_var('zend_developer_cloud.db.host')
);
return array(
'db' => array(
'driver' => 'pdo',
'dsn' => $dsn,
'username' => 'mycontainer',
'password' => 'mypassword',
),
);

Redis error "Connection read timed out" during caching

I have Zend Framework project and I decided to use Rediska as Redis client.
Rediska has cache backend adapter for ZF - Rediska_Zend_Cache_Backend_Redis.
I fetch from DB collection of objects and try to save it in cache but get error: Connection read timed out. My example of code:
$rediskaOptions = array(
'name' => 'cache',
'namespace' => 'Cache_',
'servers' => array( 'cache' => array(
'host' => Rediska_Connection::DEFAULT_HOST,
'port' => Rediska_Connection::DEFAULT_PORT,
'password' => 'qwerty'
)
)
);
$cache = Zend_Cache::factory('Core', 'Rediska_Zend_Cache_Backend_Redis',
array('lifetime' => NULL, 'automatic_serialization' => true),
array('rediska' => $rediskaOptions), false, true
);
$cacheId = 'news_main';
if (!($topics = $cache->load($cacheId))) {
$topics = DAOFactory::getInstance()->getTopicDAO()->fetchTopic(1);
$cache->save($topics, $cacheId);
}
Size of content after serialization is 26787 bytes.
Maybe Redis have size limitations for sending?
If it helps, I am using Rediska with ZF as well. Here is how I set it up.
$options = array(
'servers' => array(
array( 'host' => '127.0.0.1',
'port' => 6379,
'alias' => 'cache'
),
//'name' => 'cache',
//'namespace' => 'Cache_'
)
);
$rediska = new Rediska($options);
$frontendOptions = array('automatic_serialization' => true);
$backendOptions = array('rediska' => $rediska);
$cache = Zend_Cache::factory( 'Core',
'Rediska_Zend_Cache_Backend_Redis',
$frontendOptions,
$backendOptions,
false,
true
);
A difference I see is in the backend options. I point rediska to a rediska instance.

Categories