Yesterday I’ve been asked by a friend how we can easily http stream data from php. That was a really intersting question because it can be very useful in some case (e.g. when working with EDI). So I just made him a proof of concept.
I used socket to generate a POST HTTP requestÂ
<?php class PostRequest{ //SERVER var $address; var $path; var $port; //XML var $xmlFile; //HTTP var $boundary; function initRequest($addr, $p, $prt=80){ $this->address = $addr; $this->path = $p; $this->port = $prt; $this->boundary = md5("myString"); } function setXML($x){ $this->xmlFile = $x; } function send(){ $fs = fsockopen($this->address, $this->port); if(!$fs){ return false; } else{ $header ="POST ".$this->path." HTTP/1.0 200\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=".$this->boundary."\r\n\n"; $this->body ="--".$this->boundary."\n Content-type: text/plain\n\n".$this->xmlFile."\n--".$this->boundary."\n"; $request = $header.$this->body; fputs($fs,$request); fflush(); fclose(); return true; } } ?>
I post the code but I will update it very soon since I’ve only made few test and I think it must be tweaked
Filed under: Dev, php | Tagged: php, POST, socket, Stream, webRequest | 2 Comments »