Okay, I attached the code. Not everyone will click the paste bin link. :P

the code is a downloading function of a class.
the class has some methods need to be explained.
1) $this->debugMessage, it sends message to a call back function to a caller

2) $this->doCallback, sends the amount of bytes read to a call back function
to the caller\
3) $this->localfilename is the file where the remote file will be downloaded

4) $this->lfp is localfilepointer
5) read_chunk_size = 512
6) $headers is a predefined array of headers
7) $postdata is the url encoded data to send by post method.

Here is the code....
_________________________________________________________________________
    $this->lfp = fopen($this->localfilename,"wb");

            if($this->lfp===false){return false;}

            if(function_exists('fsockopen')){
                  $pu = parse_url($this->download_url);
                  $request="POST {$pu['path']} HTTP/1.1\r\n";
                  $request.="Host: {$pu['host']}\r\n";
                  $request.="Content-type:
application/x-www-form-urlencoded\r\n";
                  $request.=implode("\r\n",$headers)."\r\n";
                  $request.="Connection: close\r\n";
                  $request.="Content-length: ".strlen($postdata)."\r\n\r\n";


                  $sock = fsockopen($pu['host'],80,$errno,$errstr);

                  fwrite($sock,$request);

                  $this->debugMessage("Headers Sent\r\n".$request);

                  fwrite($sock,$postdata);

                  $headers="";
                  do{
                        $headers.=fread($sock,128);
                  }while(strpos($headers,"\r\n\r\n")===false);

                  $pos = strpos($headers,"\r\n\r\n");
                  $headers = split("\r\n\r\n",$headers);
                  $headers = $headers[0];

                  $this->debugMessage("Headers recieved".$headers."\r\n");
                  $this->debugMessage("Downloading started. . .\r\n");
                  $content = substr($headers,$pos+4);
                  if($content!==false){
                        $this->doCallback(fwrite($this->lfp,$content));
                  }
                  while(!feof($sock)){

$this->doCallback(fwrite($this->lfp,fread($sock,$this->read_chunk_size)));

                  }
                  $this->debugMessage("Download finished\r\n");

                  fclose($sock);

            }else {
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL,$this->download_url);
                  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                  if(!empty($this->cookiefile)){
                        curl_setopt($ch, CURLOPT_COOKIEFILE,
$this->cookiefile);
                        curl_setopt($ch, CURLOPT_COOKIEJAR,
$this->cookiefile);
                  }
                  curl_setopt($ch,CURLOPT_FILE,$this->lfp);
                  curl_setopt($ch, CURLOPT_POST,1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);

                  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

                  curl_exec($ch);
                  curl_close($ch);
            }
            fclose($this->lfp);
_________________________________________________________________________

-- 
Blog: http://talk.cmyweb.net/
Follow me: http://twitter.com/shiplu

Reply via email to