PHP predis connect to Redis Sentinel - php

I've set up a new PHP predis connection to a redis server using the documented code. I can verify it's connected ok, but I don't know how to simply test if the connection exists.
$options = array(
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => $port,
'instance' => 'myinstance',
'timeout' => '0.100' // possibly add this to connections?
);
$sentinels = [
"tcp://{$options['host']}:{$options['port']}?timeout={$options['timeout']}",
];
$connection = new Predis\Client($sentinels, [
'replication' => 'sentinel',
'service' => $options['instance'],
]);
if ($connection->isConnected() === true) {
// undocumented-- I don't think this works?
echo "connected";
} else {
echo "failed"; // this is what gets echoed
}
Is there a method to test if the connection is good short of actually reading / writing to it? isConnected() doesn't appear to work.

As suggested by #drot:
$options = array(
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => $port,
'instance' => 'myinstance',
'timeout' => '0.100' // possibly add this to connections?
);
$sentinels = [
"tcp://{$options['host']}:{$options['port']}?timeout={$options['timeout']}",
];
$connection = new Predis\Client($sentinels, [
'replication' => 'sentinel',
'service' => $options['instance'],
]);
// do this:
$connection->connect();
if ($connection->isConnected() === true) {
// undocumented-- I don't think this works?
echo "connected";
} else {
echo "failed"; // this is what gets echoed
}

Related

Cannot connect to SAP using PHP sapnwrfc with Connection Type Group

I cannot connect to SAP using PHP sapnwrfc, this is my code :
use SAPNWRFC\Connection as SapConnection;
use SAPNWRFC\Exception as SapException;
$config = [
'GROUP' => '*****', // Group/Server
'MSHOST' => '*****', // Message Server
'CLIENT' => '*****', // System ID
'SYSNR' => '*****', // Instance Number
'USER' => '*****', // Username
'PASSWD' => '*****', // Password
'SAPROUTER' => '*****', // SAP Router
'TRACE' => SapConnection::TRACE_LEVEL_OFF, // Trace
];
try {
$c = new SapConnection($config);
$f = $c->getFunction('*****');
$result = $f->invoke();
echo "<pre>";
print_r($result);
echo "<pre>";
} catch(SapException $ex) {
echo 'Exception: ' . $ex->getMessage() . PHP_EOL;
}
Is there something wrong? Please help me.
Thankyou in advance.

How can I use SoapClient in my Php Programm

How can I use the SoapClient with the method "sites_web_domain_add" to interact with the ispconfig api? My Code produces a folowing error:
error
Also soap client/server is set to enabled.
If i execute the code in my terminal it just works fine but not in my controller, so have I missed something? Like importing the right soap?
Thanks for helping!
$username = 'apiuser';
$password = '1234';
$soap_location = 'https://localhost:8080/remote/index.php';
$soap_uri = 'https://localhost:8080/remote/';
$context = stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
]
]
);
$client = new SoapClient(
null,
[
'location' => $soap_location,
'uri' => $soap_uri,
'trace' => 1,
'exceptions' => 1,
'stream_context' => $context
]
);
try {
if ($session_id = $client->login($username, $password)) {
echo "Login successful. Session ID: $session_id<br>";
}
//* Set the function parameters.
$client_id = 1;
$params = [
'server_id' => 1,
'ip_address' => '*',
'domain' => 'test2.int',
'type' => 'vhost', // vhost | alias | vhostalias | subdomain | vhostsubdomain
];
$client->sites_web_domain_add($session_id, $client_id, $params, $readonly = false);
if ($client->logout($session_id)) {
echo "Logged out.<br>";
}
} catch (SoapFault $e) {
echo $client->__getLastResponse();
die("SOAP Error: {$e->getMessage()}");
}

PHP AWS AssumeRole with InstanceProfile throws Exception

Trying to get this code to work. We have it set up so we shouldn't need to read creds from a file. But it's still looking for them.
$provider = \Aws\Credentials\CredentialProvider::instanceProfile();
call_user_func( $provider )->wait();
$config = [
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2011-06-15',
'credentials' => $provider,
'http' => [
'connect_timeout' => 30, // By default these wait indefinitely
'timeout' => 60,
]
];
try {
$stsClient = new StsClient($config);
$stsResult = $stsClient->assumeRole([
'RoleArn' => 'arn:aws:iam::1234:role/my-role',
'RoleSessionName' => 'MySession'
]);
} catch (\Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
But instead of picking it up from the instance, it's throwing an exception:
Cannot read credentials from /home/user/.aws/credentials
Remove
'profile' => 'default',
From the config.Using this means that it will always try to read from the filesystem for the credentials.

Edit the array and save the file as a new php file

There is a file named default.Db.php just containing:
<?php
// OLD FILE
function get_DbConfig(){
$config = array (
'source' => 'array',
'host' => 'DATABASE_HOST',
'port' => 'DATABASE_PORT',
'username' => 'DATABASE_USER',
'password' => 'DATABASE_PASSWORD',
'database' => 'DATABASE_NAME'
);
return $config;
}
require_once './../Common/php/face.php';
?>
What is way to replace $config inside get_dbConfig with my own array? For example, an array where each key has a specific value. After replacing the values inside this array, I will rename the file to Db.php
<?php
// NEW FILE
function get_DbConfig(){
$config = array (
'source' => 'array',
'host' => 'localhost',
'port' => '3306',
'username' => 'foo',
'password' => 'bar',
'database' => 'foobar'
);
return $config;
}
require_once __DIR__.'./../Common/php/OperateDB/DbMgrInterface.php';
?>
I don't know why you want to do this way. There are better methods to achieve this. However, you could do the following:
try {
// Read the whole file into memory
$fileStr = file_get_contents('../default.Db.php');
// Replace each string with a valid value
$fileStr = str_replace('DATABASE_HOST', HOST, $fileStr);
$fileStr = str_replace('DATABASE_USER', USER, $fileStr);
$fileStr = str_replace('DATABASE_PASSWORD', PASSWORD, $fileStr);
$fileStr = str_replace('DATABASE_NAME', DB_NAME, $fileStr);
$fileStr = str_replace('DATABASE_PORT', PORT, $fileStr);
// Write the modified content
file_put_contents("../default.Db.php", $fileStr);
// Rename default.Db.php to Db.php
$isRenamed = rename('../default.Db.php','../Db.php');
if($isRenamed) {
require_once './../Db.php';
}
}catch(Exception $exc) {
}
First you have to pass argument in your function. after that when you call that function, at that time you have to pass your array in that and assign array value with assign variable.
<?php
function get_DbConfig($configdata){
$config = array (
'source' => $configdata ['array'],
'host' => $configdata['DATABASE_HOST'],
'port' => $configdata['DATABASE_PORT'],
'username' => $configdata['DATABASE_USER'],
'password' => $configdata['DATABASE_PASSWORD'],
'database' => $configdata['DATABASE_NAME']
);
return $config;
}
require_once './../Common/php/face.php';
?>

Getting symfony query and result cache to work against oracle 11g

I'm trying to get the memcache query/result cache working with Oracle. It works flawlessly against mysql (verified with memcached console: ./memcached -u nobody -m 40 -vv). Here's what's in web/index.php:
$servers = array(
'host' => 'localhost',
'port' => 11211,
'persistent' => false
);
$cacheDriver = new Doctrine_Cache_Memcache(array(
'servers' => $servers,
'compression' => false,
'prefix' => 'qc-')
);
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine::ATTR_RESULT_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE_LIFESPAN, 3600);
This works as supposed against MySQL but fails with following message at the first location where i use ->useResultCache(true):
Result Cache driver not initialized.
Does anyone have clue about what's happening and/or if there's additional configuration needed to get it working with Oracle DB backend?
Thanks.
you should try to set those manager attributes inside your YOUR_APPConfiguration class like explained here : http://www.funstaff.ch/2009/03/19/le-cache-de-resultat-avec-doctrine
class frontendConfiguration extends sfApplicationConfiguration
{
public function configureDoctrine(Doctrine_Manager $manager)
{
$servers = array(
'host' => 'localhost',
'port' => 11211,
'persistent' => false
);
$cacheDriver = new Doctrine_Cache_Memcache(array(
'servers' => $servers,
'compression' => false,
'prefix' => 'qc-')
);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE_LIFESPAN, 3600);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE, $cacheDriver);
$manager->setAttribute(Doctrine_Core::ATTR_RESULT_CACHE_LIFESPAN, 3600);
}
}

Categories