[RESOLVED] How to do a PUT command for an API?
I'm trying to use a REST API. From the API documentation:
But this is not PHP code. How do I send this request in PHP?
A sample XML request is shown below:
Is there a PUT command in PHP?
You can do it via the cURL functions.
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?
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.
It's not oAuth or anything, I just need to pass a parameter called "Authorization" and use my token.
I used to have:
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.
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.
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.
$headers = array(
$file = fopen('php://memory', 'rw');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($xml));
curl_setopt($ch, CURLOPT_INFILE, $file);
$result = curl_exec($ch);
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)