Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts

    How does curl work when programming an api?

    Hi
    I just read this article (http://particletree.com/features/how...r-web-service/) which is about making an api with php. The person who wrote it has created a file as follows:

    file: api/delete.php

    PHP Code:
        require_once('manage.php');
        if(
    hasPermission($_POST['api_key'])
        {
           
    $m = new manage($_POST['entry_id']);
           
    $m->deleteEntry();
        } 
    And when he uses curl, he wrote like this:

    PHP Code:
       $ch curl_init("http://site.com/api/select"); 
       
    curl_setopt($chCURLOPT_HEADER0);
       
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
       
    curl_setopt($chCURLOPT_POSTFIELDS$request);
       
    $response curl_exec($ch);
       
    curl_close ($ch); 
    My question is how does the api know that it should call the delete.php file when he is using select in curl_init(...)?
    Shouldn´t it be: $ch = curl_init("http://site.com/api/delete.php");
    Last edited by earni23; 09-24-2010 at 04:13 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I guess it depends on what is in $request.
    SELECT could be a centralized page that runs and detects the action to perform, something like:
    PHP Code:
    <?php

    switch ($_POST['action'])
    {
        case 
    'delete':
            include 
    'delete.php';
            break;
        ...
    }
    ?>
    The article does indicate that the user should attach to delete.php though:
    This will allow users to send a POST request to us at http://site.com/api/delete.php with an api_key and an entry_id.
    My personal preference is SOAP which is more robust than rest.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    95
    Thanks
    14
    Thanked 0 Times in 0 Posts
    But if "SELECT could be a centralized page" shouldn´t Select end with a ".php"

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by earni23 View Post
    But if "SELECT could be a centralized page" shouldn´t Select end with a ".php"
    That would depend on the rules set up by the htaccess (if available).
    As it sits right now you are right, using that should try to access /api/select/index.php if that's the default type to serve. This differs from the SOAP approach (sorry, gots a little confused first thing in the morning :P), where your action is dictated in a fashion very similar (/service/delete for example).
    Took a quick read over the article, but I can't go through all the comments. Its possible its a typeo >.<
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    earni23 (09-24-2010)


  •  

    Posting Permissions

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