GET is working, but POST does not. This is because it fails via Postman API to the Laravel migrations database.
function add_user(Request $request)
{
$user = new user_a;
csrf_token();
$user->user_name = $request->user_name;
$user->password = $request->password;
$res = $user->save;
if ($res) {
return ["operationn" => "Done"];
} else {
return ["operation" => "failed_404"];
}
}
in API file:
Route::post('registeruser',[dynamic_small_controller::class,'add_user']);
You are trying to send the password in integer format so it is not accepting try with giving password also in ""
{
"user_name":"yahay",
"password":"8888"
}
Related
I'm using Laravel 5.4 and Socialite to allow the visitor of my site to log in.
Situation
I recently obtained the user_events permission cause I wanted to add some functionalities to my website.
Before this, some users got registered in my database along with their user token in the database. (token than includes the default permissions but not user_events)
I updated the SocialAuthController.php to reflect the new permission on the new created user and this is working great
return Socialite::driver('facebook')
->scopes(['public_profile', 'user_events'])
->redirect();
Problem
If a user is already registered in the database with his token, it is impossible to run this command $fb->get('me/events') since the token does not include the user_events permissions.
Questions
Is there a way to force a user to grab a new token with a new permission without having to remove him from the database ? ( I have data associated with users) ?
SocialAuthController
public function handleProviderCallback(SocialAccountService $service)
{
$user = $service->createOrGetUser(Socialite::driver('facebook')->user());
}
SocialeAccountService
public function createOrGetUser(ProviderUser $providerUser)
{
$account = SocialAccount::whereProvider('facebook')
->whereProviderUserId($providerUser->getId())
->first();
if ($account) {
return $account->user;
} else {
$account = new SocialAccount([
'provider_user_id' => $providerUser->getId(),
'provider' => 'facebook',
'nickname' => $providerUser->getNickname(),
'avatar' => $providerUser->avatar_original,
'token' => $providerUser->token,
]);
$user = User::whereEmail($providerUser->getEmail())->first();
if (!$user) {
$user = User::create([
'email' => $providerUser->getEmail(),
'name' => $providerUser->getName(),
]);
}
$account->user()->associate($user);
$account->save();
return $user;
}
I have been looking for any solution for my case, but I haven't found it. Therefore I decided to share my solution.
CASE
I want to share some user information between domains. It means I want to get all collected info about user who already visited my web1.com last week and come to web2.com right now. The user is for first time at web2.com but I already know who he is.
SOLUTION:
Requirements:
PHP server - central server whose generate cookies and serve user data
Database server (optionally) - keep cookies and user data (you can use file etc.) I'm using Postgres.
Possibility to include part of JS code into webs.
PHP server http://cookie-server.local index.php:
<?php
$hash = array_key_exists('my-cookie', $_COOKIE) ? $_COOKIE["my-cookie"] : NULL;
try {
$connection = new PDO("pgsql:dbname=cookie;host=localhost", 'postgres', 'postgres');
$data = findHash($connection, $hash);
if ($data) {
setcookie('my-cookie', $data['hash'], strtotime("+1 year"));
sendResponse($data);
} else {
$hash = generateHash();
$data = storeHash($connection, $hash);
setcookie('my-cookie', $hash, strtotime("+1 year"));
}
} catch (PDOException $e) {
echo $e->getMessage();
die();
}
function findHash($connection, $hash) {
$sql = 'SELECT * from cookie WHERE hash = :hash';
$stm = $connection->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$stm->execute(array(':hash' => $hash));
$result = $stm->fetchAll(PDO::FETCH_ASSOC);
if ($result === FALSE) {
printError($stm->errorInfo());
}
return count($result) > 0 ? $result[0] : NULL;
}
function sendResponse($data) {
header('Content-Type: text/javascript; charset=utf8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Max-Age: 3628800');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
echo 'showData('. json_encode($data) .');';
}
function printError($error) {
echo 'SQL error: ' . $error[2];
die();
}
function generateHash() {
return $hash = md5(uniqid(mt_rand(), TRUE));
}
function storeHash($connection, $hash) {
$sql = "INSERT INTO cookie (id, hash) VALUES (nextval('cookie_id_seq'), :hash)";
$stm = $connection->prepare($sql);
$result = $stm->execute(['hash' => $hash]);
if ($result === FALSE) {
printError($stm->errorInfo());
}
return [
'id' => $connection->lastInsertId(),
'hash' => $hash,
'name' => ''
];
}
?>
Basic web page on web1.com (The JS code hast to be everywhere you need to know info about user)
<html>
<body>
WEB 1:<br> <span id="hash"></span>
</body>
<script type="text/javascript">
function showData(data) {
document.getElementById('hash').innerHTML = "<br>ID: " + data.id + "<br>Hash: " + data.hash + "<br>Jmeno: " + data.name;
}
var script = document.createElement("script");
script.type = 'application/javascript';
script.src = "http://cookie-server.local";
document.getElementsByTagName("head")[0].appendChild(script);
</script>
<script type="text/javascript" src="http://cookie-server.local">
</html>
Database:
How it works?
When user visit web1.com, the JS code execute and include
<script type="text/javascript" src="http://cookie-server.local"> to page head element. The browser try to download content of file and it execute PHP code on server. The server look at passed cookies and find out there is no my-cookie. Therefore it generate cookie hash, store it in database, set it to user (cookie with name "my-cookie" for domain cookie-server.local) and send user data with JSONP. For another request to server it find previously generated hash in database and only extends expiration and sends user data for sure. Since now, when this user open any other web page (web2.com...) with the JS code, you know who it is.
I want to use POST method to pass data over web server. I want to retrieve data provided by web server in JSON format. I have used following code by referring Unity Manual.
void Start ()
{
StartCoroutine (LoadLoginInfo ());
}
IEnumerator LoadLoginInfo ()
{
Debug.Log("Load Login Info");
WWWForm form = new WWWForm ();
form.AddField ("username", "admin");
form.AddField ("password", "Admin123#");
UnityWebRequest www = UnityWebRequest.Post (url, form);
yield return www.Send();
if (www.isError) {
Debug.Log (www.error);
} else {
Debug.Log ("Data: " + www.downloadHandler.text);
}
}
But after certain amount of wait, I am getting following message in console.
If I try similar thing in browser then it works perfectly.
So what is the correct way to use POST method using WebRequest object?
am trying to learn the basic CURD operations to do in RESTful web service using Jersy or Restlet.
Could anyone provide me with some examples on how to POST a word/message from ONE web application to another web application (i.e) to POST a message from the first application and then to implement GET in the second application to receive it.
Thanks,
may it's useful for you its codeigniter post web service demo
public function add_category() {
############### post parameter #######
$user_id = $this->is_require($_POST, 'user_id');
$user_token = $this->is_require($_POST, 'user_token');
$cat_name = $this->is_require($_POST, 'cat_name');
$parent_id = $this->is_require($_POST, 'parent_id');
$this->Is_authorised($user_id, $user_token, 1);
#######################################
$q = "select * from tbl_category where cat_name='$cat_name' and parent_id ='$parent_id' limit 1";
$c_exits = $this->basic->select_custom($q);
if (!empty($c_exits)) {
$this->msg = "category already exist";
$this->_sendResponse(2);
}
$inn = array(
'cat_name' => $cat_name,
'parent_id' => $parent_id,
);
$temp = $this->basic->insert_entry("tbl_category", $inn, TRUE);
if ($temp['stat'] == 1) {
$this->result['cat_id'] = $temp['last_id'];
} else {
$this->_sendResponse(0);
}
$this->_sendResponse(1);
}
Now I'm bulding game by UNITY3D. I want to send json file to server to store it in database I build server by php with Yii Framework, i have problem with send data in client [UNITY3D] and retrieve it in server [Yii]. Please help me.UNITY3D code: I want to send 'name' -> to server
var url = "http://localhost:8888/TPP/index.php/site/saveName";
var form = new WWWForm();
form.AddField( "player", "Henry" );
var download = new WWW( url, form );
print(download);
yield download;
if(download.error) {
print( "Error downloading: " + download.error );
return;
} else {
// show the highscores
Debug.Log(download.text);
}
In Yii, i tried to get data in request
public function actionSaveName() {
if(isset($_POST['name']) {
echo $_POST['name'];
} else {
echo "nothing";
}
}
Is that right?
The unity part is fine, but in yii you'll have to check for $_POST['player'] instead of $_POST['name'] because according to the AddField() documentation, the first parameter is the name of the generated form element.
If you want to have it as name then you'll have to change AddField as : form.AddField("name", "Henry");