A work project is getting close to 0.1 status. Pretty underwhelming, I know. One of the last 'milestones' for 0.1 is taking these wonderful XML documents that my web app creates (and stores in eXist and sends them to an internal web service that reads in the XML and breaks it apart for use by a wide variety of other company applications. This web service will accept documents via an HTTP PUT (you know, GET, POST and it's neglected siblings PUT and DELETE) so I dug around a bit on the web (ignoring one cranky co-worker's complaints that real programmers read RFC's, or at least *know* what an RFC is) and put together some code
-
function _publish($service, $doc) {
-
'method' => 'PUT'));
-
-
if ($response === false) {
-
return false;
-
}
-
-
// Pull out the status code from the header
-
-
if ($code == 200) {
-
return true;
-
} else {
-
return false;
-
}
-
}
The only tricky thing here really is the use of the end function to grab that last match of the status codes. I got the code for doing the preg_match from this site and it seems to work just fine. The reason to use end(...) as far as I can tell is to make sure that I only get the last match of the group. Neat little trick and the link I posted demonstrates it.
I'm also quite proud that it's cURL free, as cURL seems to be the first option that most people use when doing this kind of thing. There is some existing code that is using cURL that I *might* refactor to use the streams-driven stuff that I've been playing with.
Tags: http-put, PHP


To get the HTTP status you could also use PEAR::HTTP_Request
require_once "HTTP/Request.php";
$req =& new HTTP_Request("");
$req->setURL('http://www.example.com/');
$req->sendRequest();
$code = $req->getResponseCode();
This class supports PUT an DELETE too, but I've never used it that way.
@Simon
But that would require an external package (PEAR in this case) and I really try to avoid adding external libraries to an application if I can.
Chris Hartjes' Blog: How To HTTP-PUT A File Somewhere Using PHP...
Chris Hartjes has a quick post (but complete with code) about ......
A bit offtopic.. But what happend to all the colors and the easy readable design
Your comment
"I'm also quite proud that it's cURL free, as cURL seems to be the first option that most people use when doing this kind of thing."
caught my attention. Is there any particular reason for not wanting to use cURL? I saw in another comment that you like to avoid extensions. Any reason for that?
I am fairly new to programming, so I am interested in people's practices and preferences.
Thanks
@Chad
Well, cURL seems to require more lines of code to do the same thing, so fewer lines of code is always a good thing in my book. Just a personal preference, nothing more.
As for extensions, again, it's just personal preference. One lest thing to worry about when compiling and installing PHP (I tend to do it from scratch when it's my own stuff). There are some really good extensions out there, so don't shy away from using one that fits.
Chris, although that's a very handy dandy use of end(), but I wonder, wouldn't it have more overhead than using an array index of -1?
$code = $matches[1][-1];
I suppose it might depend on the underlying code for array indexing and end() itself, I've just always used the array indexing.
@Brandon
You may be right, but I think end(...) seems a little more intuitive that using the -1 array index. They both work, and I have used that trick (the -1 array index) in other cases.
Try as I might I couldn't get Chris's code to work in my situation. For others who might find the same issues here is a fsockopen example that worked for me.
function realSocket() {
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 12);
fwrite($fp, "PUT /somewhere/somefile HTTP/1.1\r\n");
fwrite($fp, "Host: http://www.example.com\r\n");
fwrite($fp, "Content-Length: 8\r\n\r\n");
fwrite($fp, "testdata\r\n\r\n");
echo "";
while (!feof($fp)) {
echo fgets($fp, 128);
}
echo "";
fclose($fp);
}
realSocket();