CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Location
    Belgium (Country in Europe)
    Posts
    44

    PHP REST(ful) API

    Hey,

    I'm trying to create a PHP framework that uses the MVC design pattern and REST (http://en.wikipedia.org/wiki/Represe...State_Transfer).

    I know REST uses the HTTP GET, POST, PUT and DELETE to indicate what action should be taken against the given URL.

    The PHP code to check the method/action in my ArticleController:
    PHP Code:
            $requestMethod $_SERVER['REQUEST_METHOD'];
            switch(
    $requestMethod)
            {
                case 
    'GET':
                    
    $this->show($articleid);
                    break;
                case 
    'PUT':
                    
    $this->edit($articleid);
                    break;
                case 
    'POST':
                    
    $this->create($articleid);
                    break;
                case 
    'DELETE':
                    
    $this->delete($articleid);
                    break;
            } 
    But I'm having trouble understanding how you can send PUT and DELETE requests via <a> tags and html forms?

    For example:
    HTML Code:
    <form method="post" action="/Article/2">
    <input type="text" name="content" maxlength="20" size="20" />
    <input type="submit" value="Submit" />
    </form>
    Ofcourse the action="Article/2" will be dynamic in the final result of the webpage.

    But the <form> only supports method "post" (and "get"), but I want to update (HTTP PUT) the resource. How do I do that?

    The same question for delete:

    HTML Code:
    <a href="/Article/2"> Delete this article </a>
    How can i send an HTTP DELETE to that URI instead of a GET? Do I need to use Ajax to send an XMLHttpRequest with a 'PUT' to that URL?

    Thanks in advance.
    Last edited by ultddave; July 5th, 2011 at 07:33 AM.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: PHP REST(ful) API

    An <a> tag would only every allow you to sent GET requests. The only way to simulate PUT and DELETE is to write a custom GET or POST to the processing PHP script.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Jun 2009
    Location
    Belgium (Country in Europe)
    Posts
    44

    Re: PHP REST(ful) API

    Ok, thanks for the info. I'll try it that way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured