How can I install sendgrid app in Heroku using git (PHP)? - heroku

At the top of my index.php file I have:
require('sendgrid-php/SendGrid_loader.php');
And at the bottom I use this code to send the email:
<?php
if(isset($_POST['submit']))
{
$sendgrid = new SendGrid('app11445063#heroku.com', 'password');
$mail = new SendGrid\Mail();
$mail->
addTo('email#gmail.com')->
setFrom('app11445063#heroku.com')->
setSubject('Subject goes here')->
setText('Hello World!')->
setHtml('<strong>Hello World!</strong>');
$sendgrid->
smtp->
send($mail);
}
?>
I can't get the code to send an email out. I have the proper account from SendGrid and Heroku. The required files are in my directory. Do I have the wrong path to the required file?

The issue was that the SendGrid library was not in the repository in Heroku. Just do a "git status" and add the directory to the commit and then push the directory to Heroku. Make sure you also have the correct path in the "include" in your PHP code.

I believe this has to do with Sendgrid's PHP lib. If you are using SMTP (you must if using heroku) you must also have swiftmailer installed. Otherwise the line 25 won't work in sendgrid-php/SendGrid/Smtp.php
public function __construct($username, $password){
/* check for SwiftMailer,
* if it doesn't exist, try loading
* it from Pear
*/
if (!class_exists('Swift')) {
require_once 'swift_required.php';
}

Related

Laravel - How to clear config:cache on each request

I am working on a project which will have 50 subdomains and I found a solution to load separate .env file based on a domain name and everything is working fine... now my problem is
I have to run command config:cache to clear the cache so sytem can load relevant .env file, otherwise, it keeps loading the older .env file. How can i ask system to do cache clear on each load in bootstrap/app.php file???
My Code to load .env files in bootstrap/app.php
$domain = '';
if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
$domain = $_SERVER['HTTP_HOST'];
}
if ($domain) {
$dotenv = \Dotenv\Dotenv::create(base_path(), '.env.'.$domain.'.env');
try {
$dotenv->overload();
} catch (\Dotenv\Exception\InvalidPathException $e) {
// No custom .env file found for this domain
}
}
I would advise against doing that because things WILL break.
However, to answer your question, you can use Artisan::call('config:clear') inside a middleware that you can call on each request.
But instead of doing that, you could build a middleware that detects the subdomain you're getting the request from and then call the command instead, just to avoid that extra load.
I used another way to solve this on my project. It is setting the config dynamically according to the request. The config is only valid for the current request. If the count of the dynamic config is less you can use
Config::set('myConfig.hostName', $hostName);
Before doing that you must use the package
use Illuminate\Support\Facades\Config;

Fatal error with Stripe Class 'Stripe\Stripe' not found

I'm trying to implement Stripe Checkout into my website. In local the api work normal but in host I get the error :
Class 'Stripe\Stripe' not found
Note: In my host I don't have SSH. And I added files manually with FTP.
\Stripe\Stripe::setApiKey("sk_test_XXXXXX");
$token = $request->stripeToken;
$customer = \Stripe\Customer::create([
'email' => $client->email,
'source' => $token,
]);
As mentioned you have installed Stripe library manually and uploaded on server.
To use this library include init file.
require_once('/path/to/stripe-php/init.php');
ON next step set Api key, Make sure to use test api key for testing.
\Stripe\Stripe::setApiKey( "sk_test_XXXXXX");
Make sure to download latest stripe library from Githut repo
As i have seen mentioned in the comments:
Stripe needs to be installed using (preferably) composer.
This can be done with the command: composer require stripe after having SSHed into the right directory.
You then need to include the vendor/autoload.php that is generated by composer.
In your case where you cant run composer do the following:
Download stripe`s latest release manually from the github page: https://github.com/stripe/stripe-php/releases
Then you need to include the init.php file found in the downloaded stripe-php directory like this require_once('/path/to/stripe-php/init.php');
Ensure you are running at least PHP 5.4 (Note! This version has reached its end of life. Upgrade if possible to PHP 7.2). You also need the PHP extensions curl, json and mbstring.
After having used require_once('/path/to/stripe-php/init.php'); in the file the stripe code will be running in you can then set your API key using \Stripe\Stripe::setApiKey("sk_test_XXXXXX"); and then run your code like f.ex: $customer = \Stripe\Customer::create([
'email' => $client->email,
'source' => $token,
]);
`
Please use stripe library with the below code to resolve the error
$stripe_obj = new Stripe();
$stripe = $stripe_obj->setApiKey(env('STRIPE_SECRET'));

After rename app folder and namespace artisan make shows Exception: Unable to detect application namespace

I've renamed my app folder to aplicativo and changed the namespace to aplicativo. Everything works but artisan make commands.
I changed namespace through composer.json
"psr-4": {
"aplicativo\\": "aplicativo/",
}
Plus command:
artisan app:name aplicativo
You won't get this to work. You can rename the namespace (as you did with the command), but you can't rename the directory because it's hardcoded as app.
You can see the source here.
Then... if your using composer try this command
composer dump-autoload
The good news is that Laravel has now added support for custom App path. You can override the App path by adding the following code in bootstrap/app.php (or provider).
/*...*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
// override the app path since we move the app folder to somewhere else
$app->useAppPath($app->basePath('src'));
Similarly, you can change other paths (database, language, storage, etc.). You can find all the available methods here.

Where should I install Laravel on a shared hosting server?

I have found many questions about this subject but i really got confused and i want to be carefull about this.
Ok so i installed composer in the below dir :
/home/antonis/composer/composer.phar
Now my accessible "public_html" is at :
/var/www/html/project_name/index.html
So project_name is like public_html i guess.
Where should i run the laravel installation?
Would it work if it was like this :
/home/antonis/laravel/app
/home/antonis/laravel/bootstrap
/home/antonis/laravel/config
/home/antonis/laravel/database
etc
And the public dir (where public files of laravel should be) :
/var/www/html/project_name
If you copy the project to public folder then.. .env is exposed.
All the credentials are exposed
Source code is not secure.
Use sub directory installation for laravel
follow the link
https://laravel-news.com/subfolder-install
You should read the article linked Only the public folder should be in the public webspace. Everything else should be private.
Modify this Lines from Public->index.php to the public_html .
require __DIR__.'/../director name/bootstrap/autoload.php';
$app = require_once __DIR__.'/../your director name/bootstrap/app.php';

Unable to get laravel/database and restler working with versioning

I have an existing website up and running, and now I want to add a REST interface to it in an api subdirectory. I'm not able to get this to work with versioning. I installed like so (no errors):
$ php ~/bin/composer.phar create-project laravel/database --prefer-dist api
$ cd api
$ php ~/bin/composer.phar require restler/framework 3.0.0-RC6
Then I uncommented the lines in public/index.php related to Restler and add a new API class that just echos a string. If I run this via php artisan serve and look at it through the localhost URL, then the method works.
Now I want to enable versioning, so I added these lines to public/index.php
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
And in app/controllers I created a v1 directory and moved Test.php into that. I also added a namespace directive to the file of the format namespace A\B\v1
When I restart the artisan server and query the API, I get a 404 error. I've tried as both http://localhost:8000/Test and http://localhost:8000/v1/Test
What have I forgotten to do?
Here is how I made it to work. Note the folder where I placed the api class file.
in index.php
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
$r = new Restler();
$r->addAPIClass('A\B\Test');
Test.php kept in app/controllers/A/B/v1/Test.php
<?php namespace A\B\v1;
class Test
{
public function get()
{
return 'working';
}
}
Both http://localhost:8000/v1/test and http://localhost:8000/test return "working"

Resources