www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] How to do a PUT command for an API?

  1. #1
    Join Date
    Mar 2008
    Posts
    70

    resolved [RESOLVED] How to do a PUT command for an API?

    I'm trying to use a REST API. From the API documentation:
    A sample XML request is shown below:
    PUT: https://api.thesitethatimusing
    Authorization: mytokeniputhere
    Accept: application/xml
    Host: api.thesitethatimusing
    Content-Length: 193
    <contentload>
    <path>http://blah</path>
    <type>8</type>
    </contentload>
    But this is not PHP code. How do I send this request in PHP?
    Is there a PUT command in PHP?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    You can do it via the cURL functions.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2008
    Posts
    70
    Any exmaple code? Do I use CURLOPT_POST? CURLOPT_PUT? How do I send the XML? How do I send the "Authorization" token to the API?

  4. #4
    Join Date
    Mar 2011
    Posts
    65
    1.) Please think a tiny bit before posting: Since you need PUT, you will not use the _POST, but the _PUT option.

    2.) How authentication is done depends on the REST-API you are trying to use.. Loads of sites use oAuth, just use google to find some tutorials on how to do the authorization.

  5. #5
    Join Date
    Mar 2008
    Posts
    70
    It's not oAuth or anything, I just need to pass a parameter called "Authorization" and use my token.

    I used to have:
    file_get_contents("http://api.thesitewiththeapi.com?action=contentload&Authorization=4829234&type=8&path=http://myfile");

    And it worked fine and it took me 5 minutes to code. But they changed the API and it doesn't work anymore and I apparently have to use cURL now. I've already spent more than all day studying cURL documentation and getting nowhere. I just need to convert the above line to cURL. Insead of url paramaters I apparently have to use XML or JSON.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    This looks like a pretty good summary of the cURL part: http://www.gen-x-design.com/archives...quests-in-php/ . Basically, for the PUT method, it creates a file handle in memory where you would (temporarily) store your data (XML, JSON, whatever), then access via the curl_setopt() functions as demonstrated in the PUT example there.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Mar 2008
    Posts
    70
    Thanks nogdog that helped me finally figure it out.

    Here's my code since a lot of people are probably going to google this and land here. This is bascially how to send XML to an API.
    PHP Code:
            $headers = array(
                
    "Authorization: TOK:$token",
                
    "Accept: application/xml",
                
    "Content-Type: application/xml",
                
    "Host: $api_host"
            
    );

            
    $file fopen('php://memory''rw');
            
    fwrite($file$xml);
            
    rewind($file);

            
    $ch curl_init();
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_HTTPHEADER$headers);
            
    curl_setopt($chCURLOPT_PUTtrue);
            
    curl_setopt($chCURLOPT_INFILESIZEstrlen($xml));
            
    curl_setopt($chCURLOPT_INFILE$file);
            
    $result curl_exec($ch);
            
            
    curl_close($ch);
            
    fclose($file); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles