I am new to PHP. When I tried to run the below code getting the following error.
Error Message:
PHP Fatal error: Uncaught Error: Class 'Api' not found in C:\Users\cpa\Downloads\b\vendor\php1.php:4
Stack trace:
#0 {main}
thrown in C:\Users\cpa\Downloads\b\vendor\php1.php on line 4
Api.php is there in the following location. I tried n different ways to include the class but no luck. Please let me know how to resolve this error message.
C:\Users\cpa\Downloads\b\vendor\brightlocal\api\src\BrightLocal
Code:
<?php
require 'vendor/brightlocal/api/src/BrightLocal/Api.php';
$api = new Api('key', 'secret_key');
// get a list of clients
print_r($api->call('/v2/clients/get-all'));
// get a client
print_r($api->call('/v2/clients/get', array(
'client-id' => 1059
)));
// get LSRC report list
print_r($api->call('/v2/lsrc/get-all'));
// get LSRC report
print_r($api->call('/v2/lsrc/get', array(
'campaign-id' => 50
)));
// get CT report list
print_r($api->call('/v2/ct/get-all'));
// get a CT report
print_r($api->call('/v2/ct/get', array(
'report-id' => 259
)));
There are several things that are a bit off:
Why are you in the vendor folder? This things is actually used for external libraries that you download via composer or where you place the autoloader.
The require (why not require_once ?) is in the wrong folder. As can be seen from the error message. My suggestion is that you move the frontcontroller (php1.php) out of the vendor folder and in the folder which has the vendor folder as a subfolder: C:\Users\cpa\Downloads\b and setup your system from there anew.
require 'vendor/brightlocal/api/src/BrightLocal/Api.php';
Related
I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.
I am trying to follow a WordPress tutorial by Imran Sayed - Codeytek Academy (https://www.youtube.com/watch?v=lNtw4yxEydM&list=PLD8nQCAhR3tT3ehpyOpoYeUj3KHDEVK9h) to allow wordpress to build a menu and then inject into the html. I have followed the turorials 22, 23 and 24 (from the playlist) trying to use this in my own project.
I have copied the code and folder/file structure and added in the class, helpers, singletons and autoloaders. BUT everytime I try and run the code i get
Fatal error: Uncaught Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
Error: Class 'INFOCUS_THEME\Inc\Menus' not found in /home/will/Local Sites/karenkeyinfocus/app/public/wp-content/themes/infocus/template-parts/header/nav.php on line 13
I have changed the text domain from aquila to infocus as thats whats in my project in all the locations. BUT i am completly stuck and can't work out why my code is not running.
Think this is the code thats causing the problems as it can't find the class 'INFOCUS_THEME\Inc\Menus'
<?php
$menu_class = \INFOCUS_THEME\Inc\Menus::get_instance();
$header_menu_id = $menu_class->get_menu_id( 'infocus-header-menu' );
$header_menus = wp_get_nav_menu_items( $header_menu_id );
?>
I have uploaded it to my github account and post the link here, as I thought that is the best way.
https://github.com/wkey1980/infocus
When declaring a variable as a class you must use 'new' to initiate the class.
$menu_class = new \INFOCUS_THEME\Inc\Menus();
//and then you can do
$menu_instance = $menu_class->get_instance();
I want to divide the code I have written into smaller manageable files. One file has over 2500 lines and I think it might be better to put some of its code into a separate header file. However, as soon as I separate the code and run it. I get the following error:
Uncaught Error: Class 'Document' not found
Here is the code of my large-file.php:
<?php
require_once("common-head.php");
/* Some more code */
$document = new Document($document_html);
Here is the code of my common-head.php:
<?php
/* Some code */
require_once('vendor/autoload.php');
use DiDom\Document;
/* Some more code */
Both the files are located in the same directory so the path to vendor/autoload.php does not change. However, if the code is placed in separate files as I have shown above, I get the error:
Uncaught Error: Class 'Document' not found
If I take all the code out of common-head.php and place it in my large-file.php in place of require_once("common-head.php");. It works without any error. How can I resolve this issue?
Just using use in same file.
// large-file.php
use DiDom\Document;
require_once("common-head.php");
/* Some more code */
$document = new Document($document_html);
see - PHP namespaces and "use"
in api.php i use severall classes, with autoloaders. in include them like this
use \protoware\cms\Account as Account;
use \protoware\cms\Content as Content;
use protoware\cms\Count as Count;
include __DIR__ . '/vendor/autoload.php';
Local this works fine, when i upload it on a server i get this
Europe/Brussels] PHP Fatal error: Uncaught Error: Class 'protoware\cms\Content' not found in /data/vhosts/achielvolckaert.be/wwwroot/api.php:97
Stack trace:
#0 {main}
thrown in /data/vhosts/achielvolckaert.be/wwwroot/api.php on line 97
line 97 is the first line where i open a connection:
$content = new protoware\cms\Content();
$data = $content->get_content('medisch');
I have 0 clue how to fix this, i updated autloader, checked if all files were uploaded...
Try add a \ before class name, like $content = new \protoware\cms\Content(); or just $content = new Content() since you have a use statement.
If this class is a dependency, check if its correctly installed (inside your vendor folder, i.e.). Maybe run composer install?
I'm new to Propel and I need to work with a website that was already setup with Propel (1.6.9).
I got this working on my laptop and generated without any errors. (Yay!)
After generating classes (on the same schema.xml and other related files as earlier on) there seem to be some differences. (spotted in file sizes) When I uploaded these files to the web hosting where the old files were doing a fine job, I ran into an error:
Fatal error: Uncaught exception 'PropelException' with message 'Unknown parser class "PropelArrayParser"' in /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/parser/PropelParser.php:101
Stack trace:
#0 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/om/BaseObject.php(375): PropelParser::getParser('Array')
#1 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/om/BaseObject.php(424): BaseObject->exportTo('Array', 'fieldName')
#2 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/src/controllers/UsersController.php(26): BaseObject->__call('toArray', Array)
#3 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/src/controllers/UsersController.php(26): User->toArray('fieldName')
#4 /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/framework/framework.php(196): require('/var/www/vhosts...')
#5 /var/www/vhosts/10/154462/web in /var/www/vhosts/10/154462/webspace/httpdocs/DOMAIN/vendor/propel/propel1/runtime/lib/parser/PropelParser.php on line 101
The error is referring to:
$case = new AgencyCosts();
$case->fromArray($_POST, BasePeer::TYPE_FIELDNAME);
$case->save();
So I figured something was wrong with includes. That's why I added the following to my init.php:
set_include_path(dirname(DIR) . '/vendor/propel/propel1/runtime/lib/parser' . PATH_SEPARATOR . get_include_path());
require dirname(DIR) . '/vendor/propel/propel1/runtime/lib/parser/PropelJSONParser.php';
Without any success.
The init.php (where the including is done) is available here.
Any help would be appreciated.
You're getting this exception when Propel cannot find the class definition. From Propel's code:
// PropelParse::getParser
if (!class_exists($class)) {
throw new PropelException(sprintf('Unknown parser class "%s"', $class));
}
Propel generates a classmap file, where the paths of all Propel's classes are written. It seems like you just get the wrong file included or wrong paths in it. Take a look at your configuration file for the classmap key:
$conf['classmap'] = include(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'classmap-<you-project-name>-conf.php');
And then examine the contents of that file. It might happen because you blindly moved the files from your computer to the server.