I would like to save the information that I am receiving in the response of a request, in this case the "access_token" field, to my mysql database, here is the code:
My controller,
here I make a post request to have the access token:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class AuthsController extends Controller
{
public function SocialAuth(Request $request)
{
$a = $request->input('auth_code');
// URL
$apiURL = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';
// POST Data
$postInput = [
'app_id' => '7112335319877287',
'secret' => '18f52730856f43ed821187bfa9283794ca360e',
'auth_code' => $a
];
// Headers
$headers = [
//...
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
echo $statusCode; // status code
dd($responseBody); // body response
}
}
Response of my request, the value that I want to save to mysql is the access token
^ array:4 [▼
"code" => 0
"message" => "OK"
"request_id" => "202211281314430102451411010AF4AA0A"
"data" => array:3 [▼
"access_token" => "fbcaa610339b7aeb39eabf29346d06a4e7fe9"
"advertiser_ids" => array:1 [▶]
"scope" => array:18 [▶]
]
]
How can I save the access token in mysql?
create a table with the following columns, for storage:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTokenTableTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('token_table', function (Blueprint $table) {
$table->integer('id_token')->primary();
$table->string('token')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('token_table');
}
}
Use your token Model and save the data
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
// call your token Model class
use App\Models\TokenTable
class AuthsController extends Controller
{
public function SocialAuth(Request $request)
{
$a = $request->input('auth_code');
// URL
$apiURL = 'https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/';
// POST Data
$postInput = [
'app_id' => '7112335319877287',
'secret' => '18f52730856f43ed821187bfa9283794ca360e',
'auth_code' => $a
];
// Headers
$headers = [
//...
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
echo $statusCode; // status code
//check if status code is 200
if($statusCode == 200){
TokenTable::create([
'token' => $responseBody['data']->access_token
]);
echo 'ok';
}
}
}
or this
if($statusCode == 200){
TokenTable::create([
'token' => $responseBody['data']['access_token']
]);
echo 'ok';
}
Related
I have been using laravel for a short time, and I have been reading about good practices, I want to make an ajax json request with a validation in the form, I have seen that it is better to create a class "extend FormRequest" to better manage the information, in my case I am using validator but I don't know how to manage it in such a way to simplify the controller code as much as possible.
Currently I have:
Controller:
public function store(Request $request)
{
if($request->ajax()) {
$messages = [
'nombre.required' => 'El campo nombre es necesario',
'descripcion.required' => 'El campo descripción es necesario',
];
$rules = [
'nombre' => 'required',
'descripcion' => 'required',
'url' => 'required|alpha_dash'
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()) {
return response()->json([
'status' => 400,
'errors' => $validator->messages()//$validator->errors()
]);
} else {
$crear = new Proyecto([
'nombre' => $request->nombre,
'descripcion' => $request->descripcion,
'url' => $request->url
]);
$crear->save();
return response()->json([
'status' => 200,
'message' => 'Registro insertado con éxito'
]);
}
}else{
return view("404");
}
}
Model:
class Proyecto extends Model
{
use HasFactory;
protected $table = 'proyecto';
protected $primaryKey = 'id_proyecto';
protected $fillable = ['nombre', 'descripcion', 'url'];
public function getRouteKeyName()
{
return 'url';
}
}
js:
$('#crear_proyecto').submit(function(e){
e.preventDefault();
var formu = $("#crear_proyecto").serialize();
$.ajax({
type: "POST",
url: "proyecto/crearproyecto",
data: formu,
dataType: "json",
beforeSend:function(){
$(".loader").show();
},
success: function (data) {
$('#crear_proyecto').find('span.text-danger').remove();
if (data.status == 400) {
$.each(data.errors, function (key, error_value) {
$('#'+key).after("<span class='text-danger'>"+error_value+"</span>");
});
$(".loader").fadeOut("slow");
}else{
$(".loader").fadeOut("slow");
alert(data.message);
$('#crear_proyecto').trigger("reset");
$("#nombre").focus();
$('#modalformu').modal('toggle');
}
}
});
I do not know if the code I have previously put is a bad practice or to leave it like that, what I have been seeing is to leave the controller as light as possible by creating a RequestForm, but I do not know how to adapt it with json and ajax, the idea was to do something like that:
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
class PostProyectoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'nombre' => 'required',
'descripcion' => 'required',
'url' => 'required|alpha_dash',
];
}
public function messages()
{
return [
'nombre.required' => 'El campo nombre es necesario',
'descripcion.required' => 'El campo descripción es necesario'
];
}
}
EDIT
Now I have this code in the controller:
public function store(PostProyectoRequest $request)
{
$crear = new Proyecto([
'nombre' => $request->nombre,
'descripcion' => $request->descripcion,
'url' => $request->url
]);
$crear->save();
return response()->json([
'status' => 200,
'message' => 'Registro insertado con éxito'
]);
}
And this y RequestForm
namespace App\Http\Requests;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Validator;
class PostProyectoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'nombre' => 'required',
'descripcion' => 'required',
'url' => 'required|alpha_dash'
];
}
public function messages()
{
return [
'nombre.required' => 'El campo nombre es necesario',
'descripcion.required' => 'El campo descripción es necesario'
];
}
protected function failedValidation(Validator $validator)
{
if($this->wantsJson())
{
$response = response()->json([
'status' => 400,
'errors' => $validator->errors()//$validator->errors()
]);
}else{
$response = redirect()
->route('guest.login')
->with('message', 'Ops! Some errors occurred')
->withErrors($validator);
}
throw (new ValidationException($validator, $response))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
}
So you are actually right on track and you don't need to keep the old validation in the controller anymore. You want to do something like this.
//old
//public function store(Request $request)
//new
public function store(PostProyectoRequest $request)
{
$crear = new Proyecto([
'nombre' => $request->nombre,
'descripcion' => $request->descripcion,
'url' => $request->url
]);
$crear->save();
//you can't have two responses because the first one would run only because
return response()->json([
'status' => 200,
'message' => 'Registro insertado con éxito'
]);
//or
$response = [
'status' => 200,
'message' => 'Registro insertado con éxito'
];
return json_encode($response);
}
So is instead of using the normal Request facade, you would use the form request PostProyectoRequest you created. By doing so, you are telling the controller to pass the request through the form request which would validate it and if any of the validation fails it, it would return a validation error and wouldn't run the code in the controller. Read more about form requests here also read about early returns and returns in general
I try to take data from DB and use it in VUE script, but in console I see message
GET http://lara7.test/api/furnitura 401 (Unauthorized)
In Chrome devtools Network tab I see response
furnitura
{"message":"Unauthenticated."}
Here is my code
routes\api.php
Route::middleware('auth:api')->group( function () {
Route::resource('furnitura', 'API\FurnituraController');
});
App\Http\Controllers\API\BaseController
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
abstract class BaseController extends Controller
{
public function sendResponse($result, $message)
{
$response = [
'success' => true,
'data' => $result,
'message' => $message,
];
return response()->json($response, 200);
}
public function sendError($error, $errorMessages = [], $code = 404)
{
$response = [
'success' => false,
'message' => $error,
];
if(!empty($errorMessages)){
$response['data'] = $errorMessages;
}
return response()->json($response, $code);
}
}
App\Http\Controllers\API\FurnituraController
namespace App\Http\Controllers\API;
class FurnituraController extends BaseController
{
public function index()
{
$furnitura = Furnitura::all();
return $this->sendResponse($furnitura->toArray(), 'Furnitura retrieved successfully.');
}
}
resources\js\app.js
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
...
...
mounted() {
console.log("Vue ROOT instance mounted");
axios.get('/api/furnitura').then(response => this.furnitura = response.data);
console.log(this.furnitura);
},
Maybe, you do not pass the auth: API middleware. You are getting 401 from middleware.
maybe you use token for user validation or API user validation that token is missing
Getting error when I am trying to create dynamic form in using yii2-dynamicform. at the time of create method it is working fine but at the time of update showing the error. I have two tables one is
1.vendors &
2.vendors_more_categories
Relation is 1-* between vendors & vendors_more_categories I just refereed https://github.com/wbraganca/yii2-dynamicform this link.
<?php
namespace app\controllers;
namespace backend\controllers;
use Yii;
use app\models\Vendors;
use app\models\VendorsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use yii\filters\AccessControl;
use app\models\VendorsMoreCategories;
use backend\models\Model;
use yii\web\Response;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
/**
* VendorsController implements the CRUD actions for Vendors model.
*/
class VendorsController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['index','create', 'update', 'delete'],
'rules' => [
[
'actions' => ['index','create', 'update', 'delete'],
'allow' => true,
'roles' => ['#'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Vendors models.
* #return mixed
*/
public function actionIndex()
{
$searchModel = new VendorsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Vendors model.
* #param integer $id
* #return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Vendors model.
* If creation is successful, the browser will be redirected to the 'view' page.
* #return mixed
*/
public function actionCreate()
{
$model = new Vendors();
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
if($model->load(Yii::$app->request->post())){
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname());
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagename = Vendors::find()->orderBy('ven_id DESC')->one();
$imagename=$imagename->ven_id+1;
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$imagename.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Updates an existing Vendors model.
* If update is successful, the browser will be redirected to the 'view' page.
* #param integer $id
* #return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
//print_r($model->attributes);
$modelsVendorsMoreCategories = $model->ven_id;
if($model->load(Yii::$app->request->post())){
$oldIDs = ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id');
$modelsVendorsMoreCategories = Model::createMultiple(VendorsMoreCategories::classname(), $modelsVendorsMoreCategories);
Model::loadMultiple($modelsVendorsMoreCategories, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsVendorsMoreCategories, 'id', 'id')));
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelsVendorsMoreCategories) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
Address::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelsVendorsMoreCategories as $modelVendorsMoreCategories) {
$modelVendorsMoreCategories->vmc_ven_id = $model->ven_id;
if (! ($flag = $modelVendorsMoreCategories->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
$model->file = UploadedFile::getInstance($model, 'file');
$save_file = '';
if($model->file){
$imagepath = 'images/imgvendors/'; // Create folder under web/uploads/logo
$model->ven_business_logo = $imagepath.$model->ven_id.'.'.$model->file->extension;
$save_file = 1;
}
if ($model->save(false)) {
if($save_file){
$model->file->saveAs($model->ven_business_logo);
}
return $this->redirect(['view', 'id' => $model->ven_id]);
}
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}else {
return $this->render('update', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
}
}
/**
* Deletes an existing Vendors model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* #param integer $id
* #return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Vendors model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* #param integer $id
* #return Vendors the loaded model
* #throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Vendors::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
//Function used for deleting the images
public function actionDeleteimg($id, $field)
{
$img = $this->findModel($id)->$field;
if($img){
if (!unlink($img)) {
return false;
}
}
$img = $this->findModel($id);
$img->$field = NULL;
$img->update();
return $this->redirect(['update', 'id' => $id]);
}
//Function used for getting more sub categories for vendor
public function actionGetSubCategories()
{
$mbcid=$_GET['ven_main_category_id'];
$sbcid=$_GET['ven_sub_category_id'];
echo $mbcid;
}
public function actionLists($id)
{
$countVendors = Vendors::find()->where(['ven_contact_person_id' => $id])->count();
$vendors = Vendors::find()->where(['ven_contact_person_id' => $id])->all();
if ($countVendors > 0) {
foreach ($vendors as $vendor) {
echo "<option value='" . $vendor->ven_id . "'>" . $vendor->ven_company_name . "</option>";
}
} else {
echo "<option></option>";
}
}
}
You accessing modelsVendorsMoreCategories[0] (as an element of an array )
'model'=> $modelsVendorsMoreCategories[0],
in your DinamicForm widget but when you update the model you pass as $modelsVendorsMoreCategories this value
$modelsVendorsMoreCategories = $model->ven_id;
(don't seems an array, you must be sure this object contain an array with a proper element in 0 index))
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ?
[new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
$modelsVendorsMoreCategories should contain the VendorsMoreCategories model in actionUpdate method. In this code ($modelsVendorsMoreCategories = $model->ven_id) $modelsVendorsMoreCategories contains $model->ven_id. It is an integer value, not the VendorsMoreCategories object.
The Vendors model should contain a relation on the VendorsMoreCategories model:
class Vendors extends \yii\db\ActiveRecord
{
....
public function getVendorsMoreCategories()
{
return $this->hasMany(VendorsMoreCategories::className(), 'vendor_id'=>'id']);
}
}
And then, you should use that relation in your actionUpdate method:
$model = $this->findModel($id);
$modelsVendorsMoreCategories = $model->vendorsMoreCategories;
if(!$modelsVendorsMoreCategories) {
$modelsVendorsMoreCategories = [new VendorsMoreCategories];
}
In your actionUpdate you have:
$modelsVendorsMoreCategories = $model->ven_id;
But you should have:
$modelsVendorsMoreCategories = $model->nameOfMyRelation;
To get what's the real name, go in your $model, and look for something like:
/**
* #return \yii\db\ActiveQuery
*/
public function getNameOfMyRelation()
{
return $this->hasMany(VendorsMoreCategories::className(), ['ven_id' => 'id']);
}
If you don't have any function making the relation of this two tables, write one. If you having trouble doing that, you can always use the gii's model generator and check the Vendors model (you dont need to replace it, just preview the code).
Check your create.php file in view folder, pass required variable on
_form.php file from here as:-
<?= $this->render('_form', [
'model' => $model,
'modelsAddress' => $modelsAddress,
]) ?>
Check Your create file in view folder:
Controller:
Controller pass the parameter into create.php
return $this->render('create', [
'model' => $model,
'modelsVendorsMoreCategories' => (empty($modelsVendorsMoreCategories)) ? [new VendorsMoreCategories] : $modelsVendorsMoreCategories
]);
View:create.php
If You miss the parameter: 'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories.
It shows the Undefined variable error in _form.php page.
<?= $this->render('_form', [
'model' => $model,
'modelsVendorsMoreCategories' =>$modelsVendorsMoreCategories
])?>
View:_form.php
$modelsVendorsMoreCategories[0];
the paramater not passing before now it passing.
I am new for hooks in codeigniter. I have enabled the hooks in config file.
$config['enable_hooks'] = TRUE;
and then in hooks.php I have written my hook that is like below
$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => '');
AND the class having function is as below
class MyClass {
function MyClass() {
$this->CI = &get_instance();
require_once(APPPATH . 'config/database.php');
}
function Myfunction() {
$record = $this->CI->db->SELECT('*')
->FROM('currency')
->get()
->result();
echo "<pre>";
print_r($record);
die;
}}
but i am getting a blank page. please tell me what is wrong with me.
I think your main problem was in this area.
function MyClass() {
$this->CI = &get_instance();
require_once(APPPATH . 'config/database.php');
}
Try
Note: codeigniter 3 versions are case sensitive should be first letter upper case only on class and file name.
application > hooks > My_class.php
<?php
class My_class {
public function __construct() {
$this->CI = &get_instance();
// Auto load database
// require_once(APPPATH . 'config/database.php');
}
public function my_function() {
$query = $this->CI->db->get('currency');
$record = $query->result_array();
echo "<pre>";
print_r($record);
echo "</pre>";
}
}
Config Hook
$hook['pre_controller'] = array(
'class' => 'My_class',
'function' => 'my_function',
'filename' => 'My_class.php',
'filepath' => 'hooks',
);
Autoload the database better option
$autoload['libraries'] = array('database');
Codeigniter Hooks
Default ViewModel is not mandatory, I can return from controller just the array of data:
public function someAction()
{
//...
return array('result'=>$data);
}
But I can`t use this approach with Json. What should I do in dispatch event to wrap the results in JsonModel (for the appropriate accept header)?
Just create Base Controller for all your API controllers, and replace model in MvcEvent.
class JsonRestController extends AbstractRestfulController
{
public function onDispatch(MvcEvent $e)
{
$e->setViewModel(new JsonModel());
return parent::onDispatch($e);
}
}
You have to add a ViewJsonStrategy strategy to the view manager under your module.config.php:
'view_manager' => array(
'template_map' => array(
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
Then you can return a JsonModel in your action:
public function myAction()
{
$data = [1, 2, 3, 4];
return new JsonModel([
'data' => $data
]);
}
to get json data from controller you can echo json encoded data and exit. I use that for jquery ajax. i hope this is what you are looking for.
public function testAction()
{
$active = "some data";
echo json_encode(array('result' => $active));
exit();
}
then at jquery you can get this data like that
$.ajax({
type: 'GET',
url: '/index/time',
dataType: 'json',
error: function() {
$('#info').html('<p>Error on time calculation</p>');
},
success: function(data) {
data.result
}
});
Is really simple
Add as follows:
IndexController.php
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel; // Add this line
class IndexController extends AbstractActionController {
public function indexAction() {
// some index action
return new ViewModel();
}
public function apiAction() {
$person = array(
'first_name' => 'John',
'last_name' => 'Downe',
);
// Special attention to the next line
return new JsonModel(array(
'data' => $person,
));
}
}
api.phtml
<?php echo $this->json($this->data); ?>
Result:
{"first_name":"John","last_name":"Downe"}