How Can I use two "use" keyword on same page php - php

I am using two libraries on the same page. One is for pdf generation and one is for sending emails. But, this giving me error 500 - llease advise .
After some debugging, I found that it's phpmailer mails not working because of dompdf:
require_once 'lib/dompdf/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}

By using alias:
use \PHPMailer\PHPMailer\{PHPMailer as mailerClass, Exception as mailerException}; // PHP 7+

I think the problem in your code is the duplicate autoloader. The best way is when you install both packages over composer and use the composer autoloader.
In both libs you can find an example how to install them over composer.
composer require dompdf/dompdf
composer require phpmailer/phpmailer
Then you have to include the autoloader for composer.
require 'vendor/autoload.php';
After that you can use the autoloader to load all packages. In the DomPDF you find good examples how to use.
https://github.com/dompdf/dompdf
500 errors are a bit hard to find. You should enable your error logs and check directly your logs. In the log a php error should be shown.

Related

Installing PHPMailer with Composer [duplicate]

This question already has answers here:
Class Firebase\JWT\JWT not found
(5 answers)
Closed last year.
I feel like I am almost there. (I could be wrong though)
from the root of my apache server (/usr/local/var/www)
I ran
composer require phpmailer/phpmailer
I've now got the folder system
/usr/local/var/www/vendor/phpmailer/phpmailer
In this folder, there is a src folder, and in that folder are basically all the files I think I need to include on the php page where I want to use PHPmailer.
On the PHP page where I want to use PHPMailer, I have at the top:
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
and in the page I am calling
$mail = new PHPMailer(true); //Argument true in constructor enables exceptions
I am getting an error message
Error: Class "PHPMailer\PHPMailer\PHPMailer" not found in /usr/local/var/www/xxx.php
It seems like something isn't linking up correctly.
Alex Howansky posted a comment:
You need to source composer's autoloader, typically via something like require_once 'vendor/autoload.php';.
Ken Lee a comment:
Add the three lines: require './Exception.php'; require './PHPMailer.php'; require './SMTP.php'; after the line use PHPMailer\PHPMailer\Exception; (change to the actual path for the 3 files please) , retry and see what happens
Both suggestions fixed the problem. I'm going with the vendor/autoload.php solution.

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.

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';

JWE and PHPseclib without composer

I need to use jwe in my code. I found a couple of jwe libraries (here and here) that also requires phpseclib to be installed. However, we are not allowed to install composer in our area of work.
How do i reference the jwe and phpseclib libraries without composer? Thanks.
You could use Composer's autoloader without using the full Composer. eg.
<?php
include 'autoload.php';
$loader = new \Composer\Autoload\ClassLoader();
$loader->addPsr4('phpseclib\\', __DIR__ . '/path/to/phpseclib2.0');
$loader->register();
// insert your code here
Where autoload.php is this:
https://raw.githubusercontent.com/composer/composer/master/src/Composer/Autoload/ClassLoader.php
So at that point instead of having to code review the whole of Composer you just code review that one file.
You could also use the auto-loader by PHP-FIG:
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
<?php
include('autoloader.php');
$loader = new \Example\Psr4AutoloaderClass;
$loader->register();
$loader->addNamespace('phpseclib', __DIR__.'/phpseclib');
That said, I do think your companies policies are silly. If you're not going to trust Composer than why would you trust any third-party PHP library? So it's causing you problems with phpseclib today. What other libraries might you want to use in the future that this policy will also cause you problems?

Using Unirest php lib that has a namespace

I'm trying to use unirest, a new php lib for making rest calls.
I'd like to place it in a system-wide directory above my project. I then include it:
require_once ('../unirest-php-master/lib/Unirest/Unirest.php');
loads fine. Then I use it per the readme:
$response = Unirest::post(CSWA_URL ....
I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23
I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)
$response = Unirest\Unirest::post(CSWA_URL ....
I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!
Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?
It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).
The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.
Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.

Categories