I have two controller classes AuthenticationController
and RegisterUrlController
.
I would want to call a function in AuthenticationController
class called authorization
to return a value and use the value in the RegisterUrlController
class and function registerUrl
as the ACCESS_TOKEN
variable on the CURLOPT_HTTPHEADER array value.
See sample code;
<?php
namespace App\Http\Controllers\C2B;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class RegisterUrlController extends Controller
{
public function registerUrl(Request $request)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// The ACCESS_TOKEN should be passed from the other class
curl_setopt($curl, CURLOPT_HTTPHEADER,
array('Content-Type:application/json','Authorization:Bearer $ACCESS_TOKEN')
);
$curl_post_data = array(
//Fill in the request parameters with valid values
'ShortCode' => $request->ShortCode,
'ResponseType' => 'ResponseType',
'ConfirmationURL' => $request->confirmation_url,
'ValidationURL' => $request->validation_url
);
$data_string = json_encode($curl_post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$curl_response = curl_exec($curl);
print_r($curl_response);
echo $curl_response;
curl_close($curl);
}
}
The class generating the ACCESS_TOKEN
above is below.
How do I pass this return value to be the ACCESS_TOKEN
value on the array header above?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AuthenticationController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function authorization()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$credentials = base64_encode('statum:ub435!');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$credentials)); //setting a custom header
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
return json_decode($curl_response); //this is the response I need passed over to the other class
}
}
Question, how do I go about getting values from another controller?
Anyone?
You can use few ways to do this
Extends Class
Create Class Object
Use Namespace
Make Authentication Service and then call it