Laravel GuzzleHttp issue - php

I need to use and API from example.com
I use Laravel as a framework and to get data with API in their docs I see this:
curl https://api.example.com/api/AvailabilitySearch ^
-H "Authorization: Bearer eyJ0ehsagdasjhgdashdgaOiJIUzI1NiJ9.eyJpc3Msjhdshjdasd7676N5c3RlbXMuY29tIn0.DBR5UPxxxxxxzxzxzxzxzxzxzxyzxyzxyzxyzyxzyxyzxyxxWQ_BkS1mzxw" ^
-d VisitDate=2017-05-08 ^
-d PartySize=2 ^
-d ChannelCode=ONLINE
What I try to do is:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use GuzzleHttp;
class TestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function test()
{
$client = new GuzzleHttp\Client();
$res = $client->request('GET','https://example.com/api/AvailabilitySearch', [
'headers' => [
'Authorization' => 'Bearer eyJ0ehsagdasjhgdashdgaOiJIUzI1NiJ9.eyJpc3Msjhdshjdasd7676N5c3RlbXMuY29tIn0.DBR5UPxxxxxxzxzxzxzxzxzxzxyzxyzxyzxyzyxzyxyzxyxxWQ_BkS1mzxw',
],
'form_params' => [
'VisitDate' => '2017-05-24',
'PartySize' => '2',
'ChannelCode' => 'ONLINE',
],
]);
// You need to parse the response body
// This will parse it into an array
$res = json_decode($res->getBody(), true);
dd ($res);
}
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Now when I try to test i get:
BadMethodCallException in functions.php line 324:
Unknown method, request
How I can solve ths problem? How to use Guzzle instead cURL with Laravel framework? What is wrong in my code ... I just follow guzzle docs and API docs.

Related

Undefined property: App\Http\Controllers\PermissionController::$permission

there is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
class PermissionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function _construct(Permission $permission)
{
$this->permission = $permission ;
$this-> middleware("auth") ;
}
public function index()
{
$permissions = $this->permission::all();
return view("permission.index", ['permissions' => $permissions]);
}
public function getAllPermissions(){
$permissions = $this->permission::all();
return response()->json([
'permissions' => $permissions
], 200);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Your _construct is missing one underscore. It should be __construct.
In your code, the class property $permission was supposed to be set by the constructor, but since you mistyped it, it never happened. That is why you get the undefined property error

How to fix this error? ErrorException Use of undefined constant staff - assumed 'staff' (this will throw an Error in a future version of PHP) [duplicate]

This question already has answers here:
What does the PHP error message "Notice: Use of undefined constant" mean?
(2 answers)
Closed 2 years ago.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\staff;
class staffController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view ('staff.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'staff_name' => 'required',
'staff_dept' => 'required',
'staff_phoneNo' => 'required'
]);
$staff = new Staff([
'staff_name' => $request->get('staff_name'),
'staff_dept' => $request->get('staff_dept'),
'staff_phoneNo' => $request->get('staff_phoneNo')
]);
$staff->save();
return redirect()->route(staff.create)->with('success','Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
return redirect()->route('staff.create')->with('success','Data Added');
Minor change in your code.

Laravel excel 3.1problem Blank page when Import file

Package: maatwebsite/excel 3.1
He worked with me normally until some problems began.
The problem when i import excel file, nothing result or error. only blank page.
if add redirect code after importing, he redirect me to without data.
i cant find the problem, because i check my code & test to dump the row dump($row) but the same problem is a Blank page!
I tried to delete the package and then reinstall it the same problem.
You also check the database and the controllers and work Successfully
Imports/WordsImport:
<?php
namespace App\Imports;
use App\WordsList;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
class WordsImport implements ToModel, WithBatchInserts, WithChunkReading
{
/**
* #param array $row
*
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if (!isset($row[0])) {
return null;
}
return new WordsList([
'Translation' => $row[1],
'EnglishPos' => $row[2],
'EnglishWave' => $row[3],
'Word' => $row[4],
'SimpleWords' => $row[5],
'ArabicPos' => $row[6],
'ArabicWave' => $row[7],
'ArabicSense' => $row[8],
'Synonyms' => $row[9],
'Antonyms' => $row[10]
]);
}
public function batchSize(): int
{
return 1000;
}
public function chunkSize(): int
{
return 10000000000;
}
}
Controller .. /ImportWordsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\WordsImport;
use Maatwebsite\Excel\Facades\Excel;
class ImportWordsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('ImportWords');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'select_file' => 'required|mimes:xls,xlsx',
]);
Excel::import(new WordsImport, request()->file('select_file'));
//return redirect('/dashboard/wordslist')
// ->with('success','The word has been Uploaded successfully');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

Request Validation with Laravel 5.3

I am trying to validate my api call using laravel built-in request method.
I have used --resource to get make it REST.
OneTimePasswordController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use App\Models\OneTimePassword as Model;
use App\Http\Requests\OneTimePasswordReq;
class OneTimePasswordController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index(UserController $user)
{
//
$otps = Model::get();
return response()->json($otps);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(OneTimePasswordReq $request)
{
$insert = Model::create($request->all());
return response()->json($insert);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
$delete = Model::destroy($id);
return response()->json($delete);
}
}
OneTimePasswordReq
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
class OneTimePasswordReq 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 [
'mobile' => 'required',
'code' => 'required',
];
}
protected function formatErrors(Validator $validator)
{
return $validator->errors()->all();
}
/**
* Set custom messages for validator errors.
*
* #return array
*/
public function messages()
{
return [
'mobile.required'=>"Mobile field is required"
];
}
}
If i pass my params, with values, it gets inserted.
Now if i pass a post request with mobile field deleted, i expect a validation errors.
But the call is made to index method which fetches all data from the url.
My understanding is the request is rejected because the params is missing and the question is why its changing to index method and how do i get the errors?
Note : I am aware about $validator->fails() concept, which i don't want to put into my controller as laravel offers this.
As i tested it with postman, it was redirecting to the same route and picking as get.
If called with javascript code, the errors are displayed.
To check with postman, you need add in headers
X-Requested-With: XMLHttpRequest
Thanks to all supporters.

Laravel 5.2 Route not working ERR_EMPTY_RESPONSE

When calling a resource route, the server dies. Resulting in GET http://localhost:8000/tag net::ERR_EMPTY_RESPONSE.
Serving via php artisan serve on localhost:8000 on a *nix box. Same results for ff and Chrome.
Ajax used is $.get
Routes:
Route::group(['middleware' => 'auth'], function(){
Route::resource('business', 'BusinessesController');
Route::resource('business.show', 'BusinessesController');
Route::resource('business.create', 'BusinessesController');
Route::resource('business.update', 'BusinessesController');
Route::get('order', ['as' => 'order.default', 'uses' => 'BusinessesController#create']);
Route::post('order', ['as' => 'order.post', 'uses' => 'BusinessesController#signUp']);
Route::resource('tag', 'TagController');
});
Route::auth();
All routes work, with the exception of 'tag.' Moreover, running php artisan route:list results in a segmentation fault.
The following works:
Route::get('tag', function(){
$tags = App\Tag::get()->toArray();
return $tags;
});
The controller:
<?php
namespace App\Http\Controllers;
use App\Http\Middleware\TagService;
use Illuminate\Http\Request;
use App\Http\Requests;
class TagController extends Controller
{
private $_ts;
public function __construct(TagService $tagService)
{
$this->_ts = $tagService;
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
dd('test');
return $this->_ts->get();
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
The request doesn't seem to make it to the controller.

Categories