Best way to do http post requests with php?

991 views
Skip to first unread message

Caleb Lewis

unread,
May 7, 2013, 11:06:27 PM5/7/13
to ang...@googlegroups.com
Hey guys! I'm still  pretty new to angular. This is my first project using it and i have a question on how i should be using it with php. I need to do some http post requests and it works just fine, but how should I be getting jobs done with php through angular? For instance: The user just submitted information and now i want to put the information in the database and give the user a message. Should i send the post data to a process page and, along with the information submitted, post the method and model that i want to use, then include and execute it? It doesn't seem very efficient to me..Can anyone give me some advice?

Luke Kende

unread,
May 8, 2013, 2:05:51 AM5/8/13
to ang...@googlegroups.com
You can post to PHP and have it return xml or json.  

Our server is built on LAMP (Linux Apache MySQL PHP), but we use angular on the front end.. makes no difference.  My suggestion would be to build a RESTful API in PHP and use angular's $resource.  If that's to big of a task, then just create a PHP page that accepts the POSTed data and returns a JSON object after the job is done...  then based on the response, you can have angular update the view or even direct the user to a different url.  

Don't let PHP deliver HTML content, only data!  Handle what to show in the callback or, more appropriately, the promise.

If you can be more specific on what you are missing as far as "post the method and model that i want to use, then include and execute it" I will try to offer more insight.

Caleb Lewis

unread,
May 8, 2013, 9:39:08 PM5/8/13
to ang...@googlegroups.com
I looked in to resources and those were exactly what i was looking for! But before my question was about routing. When data is sent to the php file, how should classes be included and methods be executed was my main question

Luke Kende

unread,
May 8, 2013, 10:17:31 PM5/8/13
to ang...@googlegroups.com
Im not sure I follow, the only calls to PHP should be from $resource or $http and the result should be JSON such that angular doesn't know about PHP and PHP doesn't know about Angular... 

When you say ' how should classes be included and methods be executed was my main question' it makes me think you are trying to echo out html and javascript from PHP which is not the right way to go... 

Say you have a form, then on submit, it should not be POSTed to a PHP url, but instead data collected by the controller on the $scope that is then provided in the $resource call to post to the API.   PHP then collects the posted data, updates the database, performs any server-side operations etc, and then echos out a JSON as a response.

Does that make sense?



On Wed, May 8, 2013 at 7:39 PM, Caleb Lewis <cale...@gmail.com> wrote:
I looked in to resources and those were exactly what i was looking for! But before my question was about routing. When data is sent to the php file, how should classes be included and methods be executed was my main question

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/BBffZHidQlE/unsubscribe?hl=en-US.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Rick Jolly

unread,
May 9, 2013, 12:24:27 AM5/9/13
to ang...@googlegroups.com
For the routing, you can use any php framework you like (Laravel, Kohana, Silex, Slim, your own, etc.). Like Luke mentioned, you will be dealing with json requests and responses. Just data. No HTML. So a simple REST ready framework might serve you well. The Slim Framework is a good one.

One thing about php is that $_POST is populated from url encoded data. By default, angular sends all request data as json - which is good. That just means that you have to read any data that is not in the url (typically post, put, or delete data) from the php://input stream. The slim framework handles this for you.

Pan Stav

unread,
May 9, 2013, 12:28:40 AM5/9/13
to ang...@googlegroups.com
How about php returning angular?

Luke Kende

unread,
May 9, 2013, 12:59:46 AM5/9/13
to ang...@googlegroups.com
It goes against everything angular was designed to do, which is to create to single page apps


On Wed, May 8, 2013 at 10:28 PM, Pan Stav <panci...@gmail.com> wrote:
How about php returning angular?

Pan Stav

unread,
May 9, 2013, 1:02:42 AM5/9/13
to ang...@googlegroups.com

Thanks for that reassurance, i can finally get php off my mind.

Caleb Lewis

unread,
May 9, 2013, 11:37:52 AM5/9/13
to ang...@googlegroups.com
What i'm asking is, when the API is called, how are methods and classes called? Because what is sent to the API is just data..how would the API know which classes and methods to call? Or if the API is the class, how does it know which methods to execute?

Luke Kende

unread,
May 9, 2013, 12:15:11 PM5/9/13
to ang...@googlegroups.com
When you say 'what methods and classes are called' I'm not sure if you are talking PHP or Angular or what...  Let me give you a small example.  

HTML:
(loaded via call to index.html (not .php) which includes angular.js, css, etc.)

<div ng-controller="userCtrl"
<form>
  <input type="text" ng-model="username">
  <input type="button" ng-click="saveUser()">
</form>

Angular Controller with Resource calls:

function userCtrl($scope, $resource, $http){
  
  //resource construction
  var User = $resource('/my_api.php')

  //defaults the form field to empty - but binded to the value 
  $scope.username = '';
  
  $scope.saveUser = function (){
    
    if ($scope.username.length > 0){
      //save data via api 
      User.$save({user:$scope.username},
        function success(data){ //data is your json response plus resource methods
          if (data.success){
            alert('saved successfully')
            //do whatever you want in angular here: change the url, hide the form and show something else, etc
          }
        }
      )
    }
    else{
      alert('Please enter a username')
    }
  }
}

PHP file at /my_api.php:

<?
  $user = $_POST['user'];

  $response->success = false;

  if ($user){
    /* do the validation and saving to database here */
    $response->success = true;
  }
  else{
    $response->success = false;
  }  

  echo json_encode($response);
?>

Rick Jolly

unread,
May 10, 2013, 12:33:21 PM5/10/13
to ang...@googlegroups.com
What is sent to the api is a url and data. The url is mapped to your php controller which handles the data, just like a classic php application. The slight difference with a RESTful app is that different REST methods (GET, POST, etc.) can/should share the same url. Your framework should detect the request method header and route accordingly.

Caleb Lewis

unread,
May 11, 2013, 6:51:27 PM5/11/13
to ang...@googlegroups.com
Ah, your example and a bit more research helps me understand. Thanks!
Reply all
Reply to author
Forward
0 new messages