Answer: I had to change the path of PREDIS_BASE_PATH to predis/lib/.
I want to load predis inside of a PHP file, but I am having trouble. I am following the guide to load predis on the predis github website (https://github.com/nrk/predis). Here is the code that I am using to load predis:
define("PREDIS_BASE_PATH", "predis/");
echo "The predis base path is: " . PREDIS_BASE_PATH . "\n";
spl_autoload_register(function($class) {
$file = PREDIS_BASE_PATH . strtr($class, '\\', '/') . '.php';
echo "The file variable is: " . $file . "\n";
if (file_exists($file)) {
require $file;
return true;
}
});
$redis = new Predis\Client(array(
'host' => 'localhost',
'port' => 6379,
));
Here is the error that I get:
Fatal error: Class 'Predis\Client' not found
Edit: What file in the predis directory should be imported? After changing the folder permissions, I am able to echo what the variable $file is holding: "The file variable is: predis/Predis/Client.php"
According to the directory listing here, https://github.com/nrk/predis, there is no开发者_运维问答 client.php file.
I use below code to connect predis on php page, and it worked fine.. below is code
<?php
require "predis/autoloader.php";
Predis\Autoloader::register();
$redis = new Predis\Client(array(
"scheme" => "tcp",
"host" => "127.0.0.1",
"port" => 6379));
?>
write the below code to call the register method:
Predis\Autoloader::register();
instead of PredisAutoloader::register();
And put your test file parallel to Predis Folder.
$redis = new Predis\Client(array(
should be
$redis = new Predis_Client(array(
I used composer to install Redis and got a but of hard time making it work. Eventually the following script worked.
define('__ROOT__', dirname(dirname(__FILE__)));
echo "Root ".dirname(dirname(__FILE__));
require_once(__ROOT__.'/vendor/autoload.php');//load all PHP dependencies
//require_once(__ROOT__.'/vendor/predis/predis/autoload.php'); //load only Redis also works
$REDIS_SERVER="127.0.0.1";
$REDIS_PORT=6379;
try
{
echo "<p>Connecting to Redis $REDIS_SERVER:$REDIS_PORT";
$redis = new Predis\Client(array(
"scheme" => "tcp",
"host" => $REDIS_SERVER,
"port" => $REDIS_PORT
));
echo "<p>Hello I am Redis";
...
Your code looks perfectly fine. I can only assume that you aren't importing the class correctly. Are you sure $file is where the code is assuming it is.
Double check it's there and set the permissions to 777 using sudo chmod -R 777 /path/to/file
and see if that works.
Reset the permissions to something more secure afterwards if it works/doesn't work.
Hope this helps
EDIT:
Download Predis.php, put it in the same directory as the file with your php code in it, and make the code look like this:
spl_autoload_register(function($class) {
$file = strtr($class, '\\', '/') . '.php';
echo "The file variable is: " . $file . "\n";
if (file_exists($file)) {
require $file;
return true;
}
});
$redis = new Predis\Client(array(
'host' => 'localhost',
'port' => 6379,
));
精彩评论