Laravel 5.1 consuming soap wsdl service using controller and model - mysql

Currently I'm using php and nusoap and wanted to convert it to Laravel.
When creating the soap calls I use data out of a mysql database.
So I think I would need a model (to get my data) and a controller (to create request).
EDIT:
<?php
namespace App\Http\Controllers;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
class SoapController extends Controller {
public function demo()
{
// Add a new service to the wrapper
SoapWrapper::add(function ($service) {
$service
->name('currency')
->wsdl('path/to/wsdl')
->trace(true);
->options(['user' => 'username', 'pass' => 'password']);
});
// Using the added service
SoapWrapper::service('currency', function ($service) {
var_dump($service->getFunctions());
var_dump($service->call('Otherfunction'));
});
}
}
from laravel-soap I couldn't find a tutorial on how to send login parameters prior to any other request. In the example 'using the added service' I see the login credentials but it doesn't work.

This is how I got soap to work in Laravel 5.1
clean install laravel 5.1
install artisaninweb/laravel-soap
create a controller SoapController.php
<?php
namespace App\Http\Controllers;
use Artisaninweb\SoapWrapper\Facades\SoapWrapper;
class SoapController extends Controller {
public function demo()
{
// Add a new service to the wrapper
SoapWrapper::add(function ($service) {
$service
->name('currency')
->wsdl('path/to/wsdl')
->trace(true);
});
$data = [
'user' => 'username',
'pass' => 'password',
];
// Using the added service
SoapWrapper::service('currency', function ($service) use ($data) {
var_dump($service->call('Login', [$data]));
var_dump($service->call('Otherfunction'));
});
}
}
Create a route in your routes.php
Route::get('/demo', ['as' => 'demo', 'uses' => 'SoapController#demo']);
If requered you can also use the model extension as described here

Related

How to load JSON file into Laravel (controller and route)

I'm new to Laravel and trying to load a JSON file into my project (which will then be used for a search function).
NEW EDIT *** It is giving me an error of "undefined constant "data""
For my Controller.php I have:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SiteController extends Controller
{
public function index() {
$results = file_get_contents("http://ftp.ebi.ac.uk/pub/databases/genenames/hgnc/json/locus_groups/protein-coding_gene.json");
$data = json_decode($results, true);
dd($data);
}
}
And for my new edited route per response below web.php I have: (still has the laravel given code in here as well).
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
Route::get('/', function () {
return view('welcome');
});
Route::get(“data”, [SiteController::class, “index”]);
In your web.php you need import your controller file:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Controller;
// in laravel 8 and 9 your need to do this with all your controllers
use App\Http\Controllers\SiteController;
Route::get('/', function () {
return view('welcome');
});
Route::get(“data”, [SiteController::class, “index”]);

Using middleware in laravel5.4

Actually, i'm new to laravel. I'm trying to make Authenticating Users manually I have a problem that there's no middleware work anyone know what's the problem
it should redirect to the homepage but it doesn't ...
here's for login
public function __construct(){
$this->middleware('auth')->except(['index','show' ]);
}
and that for posts
public function __constructor(){
$this->middleware('guest')->except(['destroy' ]);
}
You should add your middleware in app/Http/Kernel.php and this file have examples,then add the middleware in route like this Route::group(['middleware' => 'your middleware's name'], function () {....}

Can't authenticate for REST testing

I have the following virtual host name setup on xampp: reporting.dev
public function userIsAuthenticated(\ApiTester $I)
{
$I->amGoingTo('Check Authentication works');
// i've tried both of these ways to authenticate
$I->amHttpAuthenticated('my_email', 'my_password');
$I->haveHttpHeader('Authorization', 'Basic super_long_token');
$I->sendGET(Url::toRoute('/api/reports', true));
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->deleteHeader('Authorization');
}
The following request works in postman http://reporting.dev/api/reports using a Basic Auth header and the same token in the test above.
This is my api suite config:
class_name: ApiTester
modules:
enabled:
- REST:
depends: PhpBrowser
#url: /api/
part: Json
- Yii2:
part: [orm, fixtures]
configFile: 'config/web.php'
I'm using Yii2, if I remove the behaviors function from my api controller that determines the authentication as Basic Auth, I then do get a 200 response and the expected json.
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBasicAuth::className(),
'except' => [],
];
return $behaviors;
}
So I'm unsure what else I can do here or why I'm not authenticated
The problem I had here was the auth token I have in my DB was not base64 encoded.
public function userIsAuthenticated(\ApiTester $I)
{
$I->amGoingTo('Check Authentication works');
// fixed now
$I->haveHttpHeader('Authorization', 'Basic ' . base64_encode('Basic super_long_db_auth_token'));
$I->sendGET(Url::toRoute('/api/reports', true));
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK); // 200
$I->deleteHeader('Authorization');
}

React Native / Laravel - Sending a request to API

I'm implementing an mobile application that uses Laravel as an API to communicate with a database.
I want to send a username and password from my react native application to Laravel. I am using the following code to do so:
fetch("mysite.com/api/login", {method: "POST", body: JSON.stringify({username: this.state.username, password: this.state.password})})
.then((response) => response.json())
.then((responseData) => {
this.handleLogin(responseData);
})
.done();
}
But this uses JSON.stringify to send across the data. But how do I handle that inside my Laravel application? Currently, I am accepting the data by:
public function login(Request $request)
{
$username = $request->get('username');
}
However, this is obviously not working since the data being sent is in JSON format. So inside PHP, do I need to decode the JSON parameter?
if you are getting output from postman then there is an issue in the fetching part else try
public function login(Request $request)
{
$username = $request-> username;
}

Laravel setup for basic API and AJAX-calls

Laravel POST return type?
I'm setting up an API alpha version using basic auth single sign on.
Cant debug with postman/js because its not returning JSON but redirecting me instead. From what Ive read AJAX calls shouldnt be redirected? Do I have errors in the configuration?
Routes:
Route::post('/test1', 'tradesCtrl#test1']);
//Route::post('/test1', ['middleware' => 'auth.basic.once', 'uses' => 'tradesCtrl#test1']);
AJAX call:
$.post(
"http://localhost:8000/test1",
{
p1:"100a",
p2:80
},
function(data, status,jqXHR){
console.log(data);
});
Controller (this will echo HTTP (!):
public function test1(Request $request)
{
if($request->ajax()) {
echo "AJAX!";
} else {
echo "HTTP!";
}
}
Allow cross domain for now in App.php
// allow origin
header('Access-Control-Allow-Origin: *');
Kernel.php disable csrf protection by commeting out
app\Http\Middleware\VerifyCsrfToken::class
If I try to insert validation for parameters
public function test1(Request $request)
{
$this->validate($request, [
'p1' => 'Integer',
'p2' => 'Integer'
]);
echo "Serving stuff";
}
This immediately return in a 404 page not found when failing validation, probably a redirect to something else that's not working??