How to fix this error message: syntax error, unexpected '[' - codeigniter

There is this strange error when trying to login to admin page.
Try it: http://www.masterlink.co.id/admin/
Parse error: syntax error, unexpected '[' in /home/masterli/public_html/application/admin/models/Mpages.php on line 531
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '['
Filename: models/Mpages.php
Line Number: 531
Backtrace:
Line 531:
public function update_user()
{
$options = [
'roles' => 'administrator',
];
$hash = password_hash($this->input->post('password'), PASSWORD_BCRYPT, $options);
$data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password' => $hash,
'role' => $this->input->post('roles')
);
return $this->db->update('login', $data);
}
Line 531: $options = [
How to fix the error message?

Please check your php version. Use [] not work in old php versions, change your code:
$options = array(
'roles' => 'administrator',
);

Related

Laravel Sanctum login issue

I am using laravel sanctum. I have created a login API and I am getting this error when i am creating Token
ArgumentCountError: Too few arguments to function App\Models\User::createToken(), 0 passed in project\app\Http\Controllers\API\AuthController.php on line 27 and at least 1 expected in file project\vendor\laravel\sanctum\src\HasApiTokens.php on line 44
can anyone help me? Thank You
Here is my Code
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
]);
if ($validator->fails()) {
return $this->handleError($validator->errors()->first());
}
$credentials = $request->only(['email', 'password']);
if ($user = auth()->guard()->attempt($credentials)) {
$auth = auth()->user();
$result['token'] = $auth->createToken()->plainTextToken;
$result['user'] = $auth;
$res = [
'bool' => true,
'result' => $result,
'message' => "Login Successful",
];
return response()->json($res, 200);
}
return response()->json([
'bool' => false,
'message' => "Login Failed"
]);
}
the createtoken method requires input parameters. depending on the initial conditions. this is in the error.
Like this createToken($request->token_name)
Or with abilities
createToken('token-name', ['server:update'])
See doc sanctum token

laravel client api with guzzle

I'm using API from RapidAPI face verification https://rapidapi.com/HiBrainy/api/face-recognition4 and
I have difficulty using the API
this example code PHP from RapidAPI
$client = new http\Client;
$request = new http\Client\Request;
$body = new http\Message\Body;
$body->addForm(array(
'photo1' => array(
'value' => 'image2.jpg',
'data' => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAQ=='
),
'photo2' => array(
'value' => 'image2.jpg',
'data' => 'data:image/jpeg;base64,/9j/4AAQSkZ630QAMXaf//Z'
)
), NULL);
$request->setRequestUrl('https://face-recognition4.p.rapidapi.com/FaceVerification');
$request->setRequestMethod('POST');
$request->setBody($body);
$request->setHeaders(array(
'x-rapidapi-host' => 'face-recognition4.p.rapidapi.com',
'x-rapidapi-key' => $my_api_key,
'content-type' => 'multipart/form-data'
));
$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();
I applied to laravel with the guzzle package
my code
try {
...other code...
$client = new Client();
$response = $client->post('https://face-recognition4.p.rapidapi.com/FaceVerification', [
'headers' => [
'x-rapidapi-host' => 'face-recognition4.p.rapidapi.com',
'x-rapidapi-key' => $my_api_key,
'content-type' => 'multipart/form-data'
],
'multipart' => [
[
'name' => 'photo1',
'contents' => $image1,
'filename' => 'image1.jpg'
],
[
'name' => 'photo2',
'contents' => $image2,
'filename' => 'image2.jpg'
]
]
]);
}catch (\Exception $error){
dd($error);
}
I got error
#message: """
Client error: `POST https://face-recognition4.p.rapidapi.com/FaceVerification` resulted in a `400 Bad Request` response:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":4 (truncated...)
You should consider removing your API key from the code you're sharing
Try to have a look at the raw request to have further details and verify that your request have been formatted correctly

Why PHP Laravel returns `500 Server Error` for Rule->unique()?

I am using the following validation in the controller:
$request->validate([
'name' => [
'required',
'max:50',
Rule::unique('center_classes')
]
]);
I also tried the following:
$request->validate([
'name' => [
'required',
'max:50',
Rule::unique('center_classes', 'The class name exist.')
]
]);
I have the following line in the validation.php:
'unique' => 'The :attribute has already been taken.',
The rest of errors are working properly but Rule->unique() is the only one which causes Error Server instead of passing message.
[HTTP/1.1 500 Internal Server Error 287ms]
You need to send validation error manually into your code like this
//use first
use Validator;
$response = array('response' => '', 'success'=>false);
$rules = array('name' => 'unique:center_classes,name');
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
$response['response'] = $validator->messages();
}else{
//process the request
}
return response()->json($response);

Codeigniter Ci-Merchant Paypal Error

I'm trying to use ci-merchant with paypal.
here is my test controller
class Test extends CI_Controller {
function paypal_express(){
$this->load->library('merchant');
$this->merchant->load('paypal_express');
$settings = array(
'username' => '***',
'password' => '***',
'signature' => '***',
'test_mode' => true);
$this->merchant->initialize($settings);
$params = array(
'amount' => 100.00,
'currency' => 'TRY',
'return_url' => site_url('test/paypal_express'),
'cancel_url' => site_url('test/paypal_express'));
$response = $this->merchant->purchase_return($params);
if ($response->success()){
echo 'ok';
}else{
$message = $response->message();
echo('Error processing payment: ' . $message);
exit;
}
}
}
When I run site.dev/test/paypal_express, I get,
Fatal error: Call to a member function set_params() on a non-object in /var/www/ci/application/libraries/merchant.php on line 192
php version: PHP 5.5.9-1ubuntu4.3
ci version : 2.2

Why is this syntax wrong (laravel)

This is the code i use to send an email with a contact form:
my route:
Route::post('/contatti/mail','ItemController#mail');
my controller
public function mail()
{
$data = Input::all();
$rules = array(
'subject' => 'required',
'message' => 'required'
);
$validator = Validator::make($data, $rules);
if($validator->fails()) return Redirect::to('contatti')->withErrors($validator)->withInput();
$emailcontent = array (
'subject' => $data['subject'],
'emailmessage' => $data['message']
);
Mail::send('emails.contactmail', $emailcontent, function($message){
$message->to('mymail#mymail','')->subject('Contatti');
return Redirect::to('contatti')->with('flash_message', 'Mail sent');
}
}
Why this sintax is wrong? I mean, i can't nest function in a controller in laravel?
How can i do to send the mail? I should use the post and send to a route called Mail::send with that call another controller with that parameter?
EDIT: This is the error i get:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
syntax error, unexpected '}'
its a syntax error
close closure function parentheses
Mail::send('emails.contactmail', $emailcontent, function($message){$message->to('mymail#mymail','')->subject('Contatti'); });

Resources