I created a basic laravel passport authentication and created callback and redirect to authorize user below is my code
Route::get('/redirect', function () {
$query = http_build_query([
'client_id' => 4,
'redirect_uri' => 'http://localhost:8000/callback',
'response_type' => 'code',
'scope' => '',
]);
return redirect('http://localhost:8000/oauth/authorize?'.$query);
});
Route::get('/callback', function (Request $request) {
$http = new GuzzleHttp\Client;
$response = $http->post('http://localhost:8000/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => 4,
'client_secret' => 'MUqUFrtAx3ix84zFTxwqQXA5PQDY7SWwVFW9tCNX',
'redirect_uri' => 'http://localhost:8000/callback',
'code' => $request->code,
],
]);
return json_decode((string) $response->getBody(), true);
});
I followed the steps provided on https://laravel.com/docs/5.4/passport but when i click on authorize button the page keeps loading without any response and i have to restart artisan serve. What could be the possible issue?
The issue is when using php artisan serve, it uses a PHP server which is single-threaded.
The web server runs only one single-threaded process, so PHP applications will stall if a request is blocked.
You can do this solution:
When making calls to itself the thread blocked waiting for its own reply. The solution is to either seperate the providing application and consuming application into their own instance or to run it on a multi-threaded webserver such as Apache or nginx.
Or if you are looking for a quick fix to test your updates - you can get this done by opening up two command prompts. The first would be running php artisan serve (locally my default port is 8000 and you would be running your site on http://localhost:8000). The second would run php artisan serve --port 8001.
Then you would update your post request to:
$response = $http->post('http://localhost:8001/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => 4,
'client_secret' => 'MUqUFrtAx3ix84zFTxwqQXA5PQDY7SWwVFW9tCNX',
'redirect_uri' => 'http://localhost:8000/callback',
'code' => $request->code,
],
]);
This should help during your testing until you are able to everything on server or a local virtual host.
In your /redirect route you are providing same localhost:8000 . change your redirect_uri and same mistake in callback route .
Change Your
'redirect_uri' => 'http://localhost:8000/callback',
To
'redirect_uri' => 'http://localhost:8001/callback',
Note: Im running other project on port 8001 .you can change it with yours :)
try to use XAMPP or another web server instead of just php artisan serve
Related
I've two LARAVEL projects one contains API routes and second contains the views
API end point is running on port 8000 using command php artisan serve
second one is running on port 8001 using command php artisan serve --port 8001
I want to display the JSON from API end point to the view
what are the possible ways to do this
I want to perform test my JSON data for view that's why doing this
Currently doing this
$response = $client->request('GET',
'http://localhost:8000/api/generate/token', [
'headers' => [
'Accept' => 'application/json',
],
]
);
return json_encode($response);
but this returns error Client error 404 Not Found
I was facing the same issue and this is how I solved it.
1, Run this command
composer require guzzlehttp/guzzle:~6.0
2, Implement your logic like this.
$client = new \GuzzleHttp\Client();
$response = $client->request('POST',
'your endpoint', [
'headers' => [
'Accept' => 'application/json',
],
]
);
return json_encode($response);
For more details check this website
https://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request
Maybe you should run php artisan cache:clear and then run php artisan serve --port=<your_prefered_port>
I'm getting this error "Use of undefined constant SIGKILL - assumed 'SIGKILL'" from my AJAX request, that starts this artisan command ->
Artisan::call('queue:work', [
'connection' => 'database',
'--memory' => '700',
'--tries' => '1',
'--timeout' => '35000',
'--queue' => 'updates'
]);
I'm using Laravel 5.7 as framework for application.
Jobs are managed from database, configuration ->
'database' => [
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 18000,
],
Problem appeared recently.. That is weird, because troubles wasn't here before and all this "system" worked just fine. Now worker get some jobs done just fine, but then it drops to error, and writes to table "failed_jobs" in DB this ->
ErrorException: PDOStatement::execute(): MySQL server has gone away in /srv/migration-xxxxxx-xxxx-xxxxxx/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458
As DB I'm using Microsoft Azure MySQL DB. Microsoft specialist after consultation find nothing .. server is working correctly. Queries are just fine, not that big to fail.
Please help, don't know what to do, or what is wrong...
I am connecting a web with a API with https://github.com/yiisoft/yii2-httpclient.
My call is the next:
$client = new Client();
$response = $client->createRequest()
->setMethod('GET')
->setUrl('http://'.Yii::$app->user->identity->building->ip.':40793/own_getproperties')
->setData(['user_id' => Yii::$app->user->identity->user_id, 'min_date' => $min_date, 'max_date' => $max_date, 'language' => \Yii::$app->language])
->send();
In development environment (local), there is not problem, but when i upload to production environment, call does not occur.
Can you help me?
This question already has an answer here:
Override Yii2 Swiftmailer Recipient
(1 answer)
Closed 5 years ago.
During the development and testing, I want the Yii2 mailer (swiftmailer) not to send emails to actual addresses, but replace it with developers` emails. Is there any setting in config to do it?
Just set the useFileTransport to true in component configuration for the development environment and all emails will not be sent but will be saved as files so you can easily test everything.
You should set configs for prod and dev environment.
Path for dev config (if you have advanced application template): yourProject/environments/dev/common/config/main-local.php and for prod: yourProject/environments/prod/common/config/main-local.php.
You may use EmailTarget class for logs.
Example of config for dev environment to reach the clue:
return [
'bootstrap' => ['log'],
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
],
'log' => [
'targets' =>
[
[
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'except' => ['yii\web\HttpException:404'],
'message' => [
'to' => ['example#mail.ru'],
'from' => ['yourproject#mail.ru'],
'subject' => ' Errors ',
]
],
],
],
],
];
And similarly for prod environment but with different emails.
If you don't want to use EmailTarget class you may just set your emails for dev in params config here: yourProject/environments/dev/common/config/params-local.php.
And path for prod: yourProject/environments/prod/common/config/params-local.php
Params config example:
return [
'sendToEmails' => ['email1#mail.ru', 'email2#mail.ru']
];
And then you may use the variable in your code this way: Yii::$app->params['sendToEmails'] to get the array of emails to send messages to.
Don't forget to do php init command in your project after compliting your configs.
You may see the detailed docs about environments here.
I want to create a file .env:
FACEBOOK_CLIENT_ID=*****
FACEBOOK_CLIENT_SECRET=*****
and use variable for config
'facebook' => [
'class' => 'yii\authclient\clients\Facebook',
'clientId' => env('FACEBOOK_CLIENT_ID'),
'clientSecret' => env('FACEBOOK_CLIENT_SECRET'),
],
I have found the solution.
I use package vlucas/phpdotenv.
Thank for all
You can archive this by Utilizing the enviroment constants. If you are using Yii2 advanced when you initialize your application as dev or production yii sets a constant YII_ENV as either dev or production in your index.php entry script.
If you are using yii basic you can set it as per your enviroment. For example we want to define config for dev.
We will proceed and edit our /web/index.php to
defined('YII_ENV') or define('YII_ENV', 'dev');
Then in our config file we would have the following
'facebook' => [
'class' => 'dektrium\user\clients\Facebook',
'clientId' => (YII_ENV_DEV ? 'Your key when in developent' : 'Your Key if not in developement'),
'clientSecret' => (YII_ENV_DEV ? 'Your key when in developent' : 'Your Key if not in developement'),
],
Refere to this for more details on Enviroment Constants http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html#environment-constants
There's a package for Yii2 you can use for dotenv
https://github.com/yiithings/yii2-dotenv
Install:
composer require --prefer-dist yiithings/yii2-dotenv "*"
Usage:
Create a .env in project root directory
DB_HOST="localhost"
DB_NAME="yii2basic"
DB_USER="root"
DB_PASS="YourPassword"
config/db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host='. env('DB_HOST') .';dbname='. env('DB_NAME'),
'username' => env('DB_USER'),
'password' => env('DB_PASS'),
'charset' => 'utf8',
];
You could use the dotenv package offered by Symfony. https://symfony.com/doc/current/components/dotenv.html
using it in a PHP file.
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');
// You can also load several files
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');
.env file
DB_USER=root
DB_PASS=pass
accessing the variables from the .env file
$dbUser = $_ENV['DB_USER'];
// you can also use ``$_SERVER``