phpdotenv not working with me and showing blank page - php

I'm new in vlucas/phpdotenv and I want to test it before using it in my project so I made a test folder and installed vlucas/phpdotenv in it and this is my code page:
<?php
require_once realpath(__DIR__ . "/vendor/autoload.php");
use Dotenv\Dotenv;
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$app_name = getenv("APP_NAME");
echo $app_name;
?>
and there is no output I only get a blank page and when I make var_dump($app_name); I get an error
bool(false)
What is the problem?

For anyone who finds this and wonders why theres is not working. getenv was marked unsafe a while back, you must instantiate dotenv in your script with Dotenv::createUnsafeImmutable to be able to use getenv() rather than $_ENV, although it is not advised to do so.

I had the same problem as you, but I solved it with the following code.
<?php
require './vendor/autoload.php';
Dotenv\Dotenv::createImmutable(__DIR__)->load();
$stripeSecretKey = $_ENV['STRIPE_SECRET_KEY'];
echo $stripeSecretKey;
.env file content
STRIPE_SECRET_KEY=sk_test_1234567890123456

I Used an old version From Node.js and an old version from vlucas/phpdotenv and it works!

Related

Class Amp non found

I'm new on amphp and i'd like to try this very simple code first.
I downloaded amphp with composer for windows and save all folder created inside my project folder.
composer require amphp/http-client
Then my code is:
<?php
require __DIR__ . './autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Loop;
$stringa = 'http://www.google.it/';
$request = new Request($stringa, "GET");
$location = $request->getHeader("Location");
var_dump($location);
But I get always 'Fatal error: Uncaught Error: Class 'Amp\Http\Client\Request' not found'
Any suggest?
I use wamp local server with php 7.0
Also ,after, I need to yield all the code...
Here are a few things that look for:
Did the autoload.php get included correctly?
I see there is an unnecessary . before /autoload.php.
Did you use the correct class name with the correct namespace?
Did you run composer require amphp/http-client so that the libraries will be installed?
Do the library's files exist inside the vendor/amphp/http-client directory?
If you're on Windows, use \ in the require statement.
Apart from these, I can't think of why the library won't load. I hope this helps.

Laravel "Target [Illuminate\Contracts\Bus\Dispatcher] is not instantiable."

As the title itself says, I've got the following issue:
Target [Illuminate\Contracts\Bus\Dispatcher] is not instantiable..
I'm trying to use a custom script and include the default Laravel classes
require_once dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/bootstrap/app.php';
use App\Mail\ChangeInStatusMail;
use App\Jobs\SendEmail;
After using SendEmail::dispatch() I get the previously mentioned error.
Any ideas?
Since I have experience of updating from laravel 5.2 to 5.8 or so, I had to replace 'Collective\Bus\BusServiceProvider', with 'Illuminate\Bus\BusServiceProvider', in providers array in config/app.php.
This worked for me and the error no more appeared on Password reset link click
In Laravel v.5.2 the command bus has been removed. Because of this you need to install it mazually from here to make it work.

Cannot redeclare Laravel's __() function when using oscarotero/gettext translation library

I have a set of translations in PHP array. Using Oscarotero's gettext library, I'm getting an error of:
"Cannot redeclare __() (previously declared in
D:\LocaleTesting\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:907)"
when the execution of the code $t->register()
$aTranslation = Translations::fromJsonFile(public_path() . '/locale/'.$sLocale.'/LC_MESSAGES/admin.json');
$oTranslator = new Translator();
$oTranslator->loadTranslations($aTranslation);
$oTranslator->register();
Also, I've search that this error only occur when your Laravel version is 5.4 and above. Any help will do. Thank you! Please Oscarotero/gettext's github for more information about the library.
It seems this is a know issue with the library:
https://github.com/oscarotero/Gettext/issues/180
One way around it is loading the translator functions before laravel helpers are loaded (solution from the issue above):
I'm using the package with a require before vendor/autoload.php on public/index.php and artisan command.
# public/index.php
require __DIR__.'/../vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/../vendor/autoload.php';
# artisan
require __DIR__.'/vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/vendor/autoload.php';

namespace class not found

I want to use this php library "Math-php-master" in my php script.
it is using namespaces.
My script is outside the library folder and the folder for the library is "MathPHP"
There is a php file "Finance.php" which I want to access.
path is "MathPHP/Finance.php". finance.php is using namespace MathPHP
and there is function irr() inside Finance.php which I need.
My code for accessing in my script is below:
include 'MathPHP/Finance.php';
use MathPHP\Finance;
$s = new MathPHP\Finance;
$val = $s->irr(array);
but when I run the script it is giving error "Uncaught Error: Class 'MathPHP\Finance' not found"
I tried everything, I tried the code given on github by the author but it is not working at all
link to the library on github: https://github.com/markrogoyski/math-php
Please help me.
thanks
You have not gone through the effort of readying the readme on how to use the library. In PHP most projects use composer and its autoloading to load classes.
Install composer if you do not have it https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
In short as the documentation suggests do
php composer.phar require markrogoyski/math-php:0.*
And in your code
require_once(__DIR__ . '/vendor/autoload.php');
use MathPHP\Finance;
// the rest of the code here

PEAR module (HTTP_Request2) installed, but not recognized in php file?

I have HTTP_Request2 installed on my server; pear list shows it in the list of installed packages. But the following php file:
<?php
ini_set("include_path", '/path/to/php:' . ini_get("include_path"));
if (class_exists('HTTP_Request2')) {
print("true");
} else {
print("false");
}
...returns false. I've also tried replacing the ini_set line with
include '/path/to/php/HTTP/Request2.php';
...but I get the same result. Is there something I'm missing, or something else I can check?
Simply setting the include path doesn't implicitly give you access to the code. You need to either set the include path and then use relative includes:
ini_set("include_path", ...);
require_once 'HTTP/Request2.php';
Or just use fully qualified includes:
require_once '/path/to/HTTP/Request2.php';
That said, HTTP_Request2 is kinda old and I'd instead recommend using something like Guzzle via composer.

Categories