It keep saying "Object of class stdClass could not be converted to string". I'm using php curl lib to http request in API. I'm already search but I don't understand the Curl lib.
the error is when I add authorization key this error shows:
Object of class stdClass could not be converted to string
on this line of code:
['Authorization' => 'Bearer ' .$request->session()->get('user_session_key')]
this is my Controller:
public function user_department(Request $request) {
if ( empty( $request->session()->get('user_session_key') ) ) {
return redirect('/login');
}
$api_response = http_request(
'/master_file/department/getAllDepartments/',
[], ['Authorization' => 'Bearer ' .$request->session()->get('user_session_key')], 'GET'
);
dd($api_response);
return view ('user_management.user_department', [
'department_list' => $api_response->status == 200 ? $api_response->response : []
]);
}
this is my http_Request function:
function http_request($path, $body = [], $header = [], $method = 'POST') {
// Open Connection
$connection = curl_init( \Config::get('constants.api_base_url') . $path );
$header_content = ['Content-Type: application/json'];
// Define HTTP Request Method
curl_setopt($connection, CURLOPT_CUSTOMREQUEST, $method);
if ( count( $body ) > 0 ) {
$data_string = json_encode($body);
// Defind HTTP Request Body
curl_setopt($connection, CURLOPT_POSTFIELDS, $data_string);
array_push($header_content, 'Content-Length: ' . strlen( $data_string ));
}
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
// Define HTTP Request Header
curl_setopt($connection, CURLOPT_HTTPHEADER, array_merge( $header_content, $header ));
// Excute Request
$response = curl_exec($connection);
// Response
return json_decode($response);
}