Yii No data received in post method only on server - php

Created an API endpoint I which I am submitted data as the body in postman, but it is always empty on the server whereas it works fine on localhost.
Please look at the request
The same request works on localhost but not online. Here is code on the server side I am using Yii::$app->request->post() to dump the data, here is the full code of that endpoint. I had tried $_POST,$_REQUEST every request is empty on the server,
public function actionSocialcheck()
{
$data = [];
$model = new User();
var_dump(Yii::$app->request->post());
if ($model->load(Yii::$app->request->post(),'')) {
$social = User::findBySocialLogin($model->socialLogin);
if (empty($social)) {
$data['status'] = self::API_OK;
$data['check'] = false;
} else {
if (Yii::$app->request->post('role_id') != $social->role_id) {
$data['error'] = \Yii::t('app', "You don't have permission to login as " . User::getRoleOptions($social->role_id));
return $this->response = $data;
}
$data['error'] = \yii::t('app', "Social ID already exists.");
$data['check'] = true;
$data['status'] = self::API_OK;
$data['detail'] =$social;
$data['access-token'] = $social->access_token;
// $usercl=new User();
$loginarr = array(
'device_name' => Yii::$app->request->post('device_name'),
'device_token' => Yii::$app->request->post('device_token'),
'device_type' => Yii::$app->request->post('device_type'),
);
$data['login_detail']=$loginarr;
}
} else {
$data['error'] = "Data not posted.";
}
$this->response = $data;
}
When I try the same request on the local server it works fine here is the output of that
Can you please help me out in this case. Thanks

public function actionSocialcheck(){
if(Yii::$app->request->isPost){
$data = [];
$social = User::findBySocialLogin(Yii::$app->request->post('socialLogin'));
if (empty($social)){
$data['status'] = self::API_OK;
$data['check'] = false;
}else{
if (Yii::$app->request->post('role_id') != $social->role_id) {
$data['error'] = \Yii::t('app', "You don't have permission to login as " . User::getRoleOptions($social->role_id));
return $this->response = $data;
}
$data['error'] = \yii::t('app', "Social ID already exists.");
$data['check'] = true;
$data['status'] = self::API_OK;
$data['detail'] =$social;
$data['access-token'] = $social->access_token;
$loginarr = array(
'device_name' => Yii::$app->request->post('device_name'),
'device_token' => Yii::$app->request->post('device_token'),
'device_type' => Yii::$app->request->post('device_type'),
);
$data['login_detail']=$loginarr;
}
}else {
$data['error'] = "Data not posted.";
}
$this->response = $data;
}

Related

how to use dreamscape api in nodejs for check domain name

I have already work this in php. Here is my working code:
$request = array(
'DomainNames' => $domain_names
);
$response = dreamScapeAPI('DomainCheck', $request);
$available = false;
$alt_domains = array(); // Alternative to user's expected domain names
if (!is_soap_fault($response)) {
// Successfully checked the availability of the domains
if (isset($response->APIResponse->AvailabilityList)) {
$availabilityList = $response->APIResponse->AvailabilityList;
foreach ($availabilityList as $list) {
if ($list->Available){
if ($domain == $list->Item) {
$available = true; // user prefered domain found
}
else {
$alt_domains[] = $list->Item;
}
}
}
}
else {
$error = $response->APIResponse->Errors;
foreach ($error as $e) {
$api_error = $e->Message;
//echo $e->Item . ' - ' . $e->Message . '<br />';
}
}
}
function dreamScapeAPI($method, $data = null) {
$reseller_api_soap_client = "";
$soap_location = 'http://soap.secureapi.com.au/API-2.1';
$wsdl_location = 'http://soap.secureapi.com.au/wsdl/API-2.1.wsdl';
$authenticate = array();
$authenticate['AuthenticateRequest'] = array();
$authenticate['AuthenticateRequest']['ResellerID'] = '**';
$authenticate['AuthenticateRequest']['APIKey'] = '**';
//convert $authenticate to a soap variable
$authenticate['AuthenticateRequest'] = new SoapVar($authenticate['AuthenticateRequest'], SOAP_ENC_OBJECT);
$authenticate = new SoapVar($authenticate, SOAP_ENC_OBJECT);
$header = new SoapHeader($soap_location, 'Authenticate', $authenticate, false);
$reseller_api_soap_client = new SoapClient($wsdl_location, array('soap_version' => SOAP_1_2, 'cache_wsdl' => WSDL_CACHE_NONE));
$reseller_api_soap_client->__setSoapHeaders(array($header));
$prepared_data = $data != null ? array($data) : array();
try {
$response = $reseller_api_soap_client->__soapCall($method, $prepared_data);
} catch (SoapFault $response) { }
return $response;
}
I tried with this : https://github.com/dan-power/node-dreamscape
But domain search can not work properly. Mainly, I want it on nodejs using nodejs dreamscape api but this method is not available on nodejs.
Here is my working demo: click here

How to access array values in laravel

This method i am using for storing currentTime in database using ajax call and its working
public function saveTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 404]);
}
$video = \App\Lession::where('id',$lession_id)->first();
$lesson_id = $request->lession_id;
$progress = $request->time;
//save them somewhere
$watch_history = $user->watch_history;
$watch_history_array = array();
if ($watch_history == '') {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
} else {
$founder = false;
$watch_history_array = json_decode($watch_history, true);
for ($i = 0; $i < count($watch_history_array); $i++) {
$watch_history_for_each_lesson = $watch_history_array[$i];
if ($watch_history_for_each_lesson['lession_id'] == $lesson_id) {
$watch_history_for_each_lesson['progress'] = $progress;
$watch_history_array[$i]['progress'] = $progress;
$founder = true;
}
}
if (!$founder) {
array_push($watch_history_array, array('lession_id' => $lesson_id, 'progress' => $progress));
}
}
$data['watch_history'] = json_encode($watch_history_array);
$check = User::where('id',$user->id)->update(['watch_history' => $watch_history_array]);
return response()->json(['message' => 'Time saved', 200]);//send http response as json back to the ajax call
}
But when I use this another method to get the time back for playback it's getting the array inside an array
public function getTime(Request $request, $lession_id){
$user = Auth::user();
if($user === null){
return response()->json(['message' => 'User not authenticated', 403]);
}
$user_video = User::where('id',$user->id)->first();
$array = $user_video->watch_history;
foreach ($array as $key => $value) {
echo $check;
}
}
My current Output in a database is as follows
[{"lession_id":"157","progress":"71.449464"},{"lession_id":"156","progress":"92.113123"}]
So help me to get the values out of that for each lession id

Session Dying Right After Calling Auth::login()

Stackoverflow community! We've been struggling with this issue for a while now. We're attempting to upgrade our Laravel v4 site to Laravel v7. So far, it's been fine. When we use our login function, it properly authorizes the user, but upon redirect, the session gets killed. We've checked it over and over again, but we simply cannot find anything that could be the cause of the issue. Here is the suspect function:
public function loginv2()
{
if (Auth::check()) {
return Redirect::action('FrontController#showWelcome')->withMessage("Already logged in.");
}
if (!Auth::check() && !isset($_GET['token']))
{
$_SESSION['redirect'] = "https://zjxartcc.org";
header("Location: https://login.vatusa.net/uls/v2/login?fac=ZJX");
exit;
}
$token = $_GET['token'];
$parts = explode('.', $token);
$token = $this->base64url_decode($parts[1]);
$jwk = json_decode('{"alg":"HS256","use":"sig","kty":"oct","k":"..."}', true);
$algorithms = ['HS256' => 'sha256', 'HS384' => 'sha384', 'HS512' => 'sha512'];
if (!isset($algorithms[$jwk['alg']])) {
return Redirect::action('FrontController#showWelcome')->withMessage("Invalid Operation");
}
$sig = $this->base64url_encode(hash_hmac($algorithms[$jwk['alg']], "$parts[0].$parts[1]", $this->base64url_decode($jwk['k']), true));
if($sig == $parts[2]) {
$token = json_decode($token, true);
if($token['iss'] != 'VATUSA') {
return Redirect::action('FrontController#showWelcome')->withMessage("Not issued from VATUSA");
}
if($token['aud'] != 'ZJX') {
return Redirect::action('FrontController#showWelcome')->withMessage("Not issued for ZJX");
}
$client = new GuzzleHttp\Client();
$url = "https://login.vatusa.net/uls/v2/info?token={$parts[1]}";
$result = $client->get($url);
$res = json_decode($result->getBody()->__toString(), true);
$userstatuscheck = User::find($res['cid']);
if($userstatuscheck) {
if($userstatuscheck->status == 1) {
return Redirect::action('FrontController#showWelcome')->withMessage("You are not an active controller, and cannot login.");
} else {
Auth::login($userstatuscheck, true);
require("/home2/zjxartcc/public_html/forum/smf_2_api.php");
smfapi_login($userstatuscheck->id);
setcookie("ids_loggedin", $res['cid'], 0, "", ".zjxartcc.org");
$_SESSION['loggedin'] = $res['cid'];
}
} else {
return Redirect::action('FrontController#showWelcome')->withMessage("No user matching your information on the roster.");
}
//update email records and rating
$forum = SMFMember::find($res['cid']);
$forum->email_address = $res['email'];
$forum->save();
$member = User::find($res['cid']);
$member->rating_id = $res['intRating'];
$member->email = $res['email'];
$member->save();
return Redirect::action('FrontController#showWelcome')->withMessage('You have been logged in!');
} else {
return Redirect::action('FrontController#showWelcome')->withMessage("Bad Signature");
}
}
All help and suggestions are greatly appreciated! Thank you!

How to update or insert new data on codeigniter

I'm setting up a rest-API on my server, and I want to update a table (i.e "comp_holding_stock"). but every time I test to post new data it returns "No item found"
Here is my controller
public function create_comp_holding_stock(){
$returnArr['status'] = '0';
$returnArr['response'] = '';
try {
if (!$this->input->post()) {
$returnArr['response'] = "Only POST method is allowed";
} else {
$holding_stock_data = array(
'comp_id' => $this->input->post('comp_id'),
'customer_id' => $this->input->post('customer_id'),
'quantity' => $this->input->post('quantity'),
'date' => date('Y-m-d H:i:s')
);
if (!isset($holding_stock_data)) {
$returnArr['response'] = "Some Parameters are missing";
} else {
$customer = $this->Customer->save_holding_stock($holding_stock_data);
if (!$customer) {
$returnArr['response'] = 'No items found';
} else {
$returnArr['status'] = '1';
$returnArr['response'] = $customer;
}
}
}
} catch (Exception $ex) {
$returnArr['response'] = "Error in connection";
$returnArr['error'] = $ex->getMessage();
}
$response = json_encode($returnArr, JSON_PRETTY_PRINT);
echo $response;
}
And here is my model below
public function save_holding_stock($holding_stock_data)
{
// $this->db->trans_start();
$success = $this->db->insert('comp_holding_stock', $holding_stock_data);
return $success;;
}
what am i doing wrong? what is the best approach to this scenarios
I would recommend try to check if you have load model in your controller.
And in your model try to do this.
public function save_holding_stock($holding_stock_data, $comp_id=FALSE)
{
if(!$comp_id == -1 || !$this->exists($comp_id))
{
if($this->db->insert('comp_holding_stock', $holding_stock_data))
{
$holding_stock_data['comp_id'] = $this->db->insert_id();
return TRUE;
}
return FALSE;
}
$this->db->where('comp_id', $comp_id);
return $this->db->update('comp_holding_stock', $holding_stock_data);
}
Try these changes in your code
In your controller,
$customer = $this->Customer->save_holding_stock($holding_stock_data);
$save_status = $this->db->affected_rows();
if ($save_status>0) {
$returnArr['status'] = '1';
$returnArr['response'] = $customer;
} else {
$returnArr['response'] = 'No items found';
}
In your model,
public function save_holding_stock($holding_stock_data)
{
// $this->db->trans_start();
$this->db->insert('comp_holding_stock', $holding_stock_data);
}

Update query for API in codeigniter is not working

i want to write a API for updating data in sql. I am using CI for it.I am new to this field.while i have written it is not working in localhost itself .Can anyone help me? I am attaching my controller and model here.it is showing an error like this page is not working
function editprofile($id,$data) {
$this->db->where(array('user_id' => $id));
$this->db->update('registrationdetails', $data);
if(!empty($id))
{
$result = true;
} else {
$result = false;
}
return $result;
}
public function updateuser()
{
$adminId = 1;
$AUTHENTIC_KEY = "4u0IOxa1YTwNo38QjArD9ysW6PgVnbX7vtlJ";
$user_id=2;
$firstname ="aiswarya";
$lastname ="mathew";
$email ="aiswarya#gmail.com";
$password ="aiswarya";
$confirmpassword ="aiswarya";
$contactnumber ="999999999";
$gender ="female";
$address ="canada";
$department ="cse";
$designation ="swe";
$Admindetails = $this->common->CheckValidAdmin($adminId,$AUTHENTIC_KEY);
if($Admindetails != false)
{
$validAdmin = array('authentication_key' => $Admindetails->authentication_key,
'admin_id' => $Admindetails->id
);
$data = array();
$data = array('firstname'=>$firstname,'lastname'=>$lastname,'email'=>$email,'password'=>$password,'confirmpassword'=>$confirmpassword,'contactnumber'=>$contactnumber,'gender'=>$gender,'address'=>$address,'department'=>$department,'designation'=>$designation);
$status = $this->user->editprofile($user_id,$data);
if($status == true)
{
$response = array('status' => 200,
'message' => 'updated successfully',
'admin_details' => $validAdmin
);
} else {
$response = array('status' => 404,
'message' => 'unable to add, please try again',
'admin_details' => $validAdmin);
}
} else {
$response = array('status' => 404,
'message' => 'Authentication Failed');
}
echo json_encode($response);
}
function editprofile($data) {
if(!empty($data['user_id']))
{
$this->db->where(array('user_id' => $data['user_id']));
$this->db->update('registrationdetails', $data);
$result = true;
} else {
$result = false;
}
return $result;
}
public function updateuser()
{
$data['adminId'] = 1;
$data['AUTHENTIC_KEY'] = "4u0IOxa1YTwNo38QjArD9ysW6PgVnbX7vtlJ";
$data['user_id']=2;
$data['firstname'] ="aiswarya";
$data['lastname'] ="mathew";
$data['email'] ="aiswarya#gmail.com";
$data['password'] ="aiswarya";
$data['confirmpassword'] ="aiswarya";
$data['contactnumber'] ="999999999";
$data['gender'] ="female";
$data['address'] ="canada";
$data['department'] ="cse";
$data['designation'] ="swe";
$Admindetails = $this->common->CheckValidAdmin($data['adminId'],$data['AUTHENTIC_KEY']);
if($Admindetails != false)
{
$validAdmin = array('authentication_key' => $Admindetails->authentication_key,
'admin_id' => $Admindetails->id);
$status = $this->user->editprofile($data);
if($status == true)
{
$response = array('status' => 200,
'message' => 'updated successfully',
'admin_details' => $validAdmin
);
} else {
$response = array('status' => 404,
'message' => 'unable to add, please try again',
'admin_details' => $validAdmin);
}
} else {
$response = array('status' => 404,
'message' => 'Authentication Failed');
}
echo json_encode($response);
}

Categories