Get Object (PHP and ANGULARJS)

I want to get an object sent from an Angular function to a PHP program. Here is my opinion:

<ion-view>
<ion-content padding="true">
<div class="card">
  <div class="item item-divider text-center">
    Authentification
  </div>
  <div class="item item-text-wrap text-center">
    <form>
      <div class="list">
        <label class="item item-input">
          <input type="text" placeholder="Nom d'utilisateur" ng-model="user.username">
        </label>

        <label class="item item-input">
          <input type="password" placeholder="mot de passe" ng-model="user.password">
        </label>


      </div>
    </form>

  </div>
  <div class="item item-divider text-center">
    <a  href="" class="button button-positive button-small" ng-click="logIn(user)">
      <i class="ionicons ion-android-share"></i>
      Identifiez moi, vite !
    </a>
    <a  href="" class="button button-energized button-small">
      <i class="ionicons ion-android-mail"></i>
      Mot de passe perdu !
    </a>
  </div>
</div>
<button class="button button-large button-full button-positive">
  je n'ai pas de compte
</button>

Here is the controller:

'use strict';

app
.controller('homepageIndex',function ($scope) {
})
.controller('homepageLogin',function ($scope , userProvider) {
  $scope.user={};

  $scope.logIn = function (user) {
    console.log($scope.logIn);
    console.log($scope.user);
    userProvider.logIn(user);
  }


 })
 ;

Here is my userProvider.js

'use strict';

app.factory('userProvider', function ($rootScope , $http) {

  function logIn(user) {
    var url='http://127.0.0.1:100/suitecrm/service/rest.php';

    $http.post(url,user)
      .success(function (response) {
      console.log(response);
        console.log(url);

      });

  }

  return {
    logIn: logIn
  }
});

In my file rest.phpI want to get this user object that contains the username and password:

$username =$_POST['username'];
$password =$_POST['password'];

This method does not work. I want to know how to get username and password in my rest.php Thanks for ur help.

+4
source share
2 answers

@Mohit Tanwani

$request = json_decode(file_get_contents('php://input'));

You can put this at the beginning of your PHP script. $ request will be a stdClass object with data as properties.

$username = $request->username;
$password = $request->password;

Alternatively, if you prefer to work with it as a destination array, use:

$request = json_decode(file_get_contents('php://input'), TRUE);

and access the following data:

$username = $request['username'];
$password = $request['password'];
+1

.

var req = {
 method: 'POST',
 url: 'http://127.0.0.1:100/suitecrm/service/rest.php',
 data: { username: 'username', password:  'password' }
}

$http(req).then(function(){
   //Success
}, function(){

});
+3

Source: https://habr.com/ru/post/1652054/


All Articles