Some PHP and cURL Help?

Discussion in 'PHP' started by C. Berntsen, Jul 15, 2008.

  1. #1
    Please move or delete this topic if it is inappropriate or wrong placed.

    Alright so I'm stuck with some coding here, and I can't figure it out. I'm no expert in cURL, and I know hardly anything about, most of the cURL is taken a bit from here and there.

    Anyway to the case. (host to connect to is as you probably can see in below code, rapidshare.com).

    $rslink = "http://rapidshare.com/files/129518071/LimeWire_Pro_4.18.3_.rar";
    $link = "http://rapidshare.com/files/129518071/LimeWire_Pro_4.18.3_.rar";
    $prem = "user-pass"; // Not posting my real Premacc, just example
    $fsize = "7490560"; // Bytes
    $type = "free";
    
    downloadfree($rslink,$fsize,$prem);
    
            function downloadfree($link,$filesize,$prem)
        {
        	ob_start();
        	// RS link
            $url = @parse_url($link);
            $filename = basename($link);
            if($link == "")
            {
                @header("HTTP/1.0 404 Not Found");
                echo "File Not Found";
                exit;
            }
            @header("Content-Type: application/octet-stream");
            @header("Content-Disposition: attachment; filename=".$filename);
            @header("Content-Length: ".$filesize);
            $vars = "dl.start=PREMIUM&uri={$url['path']}&directstart=1";
            $head = "Host: {$url['host']}\r\n";
            $head .= "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n";
            $head .= "Cookie: user={$prem}\r\n";
            $head .= "Content-Type: application/x-www-form-urlencoded\r\n";
            $head .= "Content-Length: ".strlen($vars)."\r\n";
            $head .= "Connection: close\r\n\r\n";
            $fp = @fsockopen($url['host'], 80, $errno, $errstr);
            	$curl = curl_init();
    			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    			curl_setopt($curl, CURLOPT_HEADER, 1);
    			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    			curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; nl; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14");
    			curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
    			curl_setopt($curl, CURLOPT_URL, "http://rapidshare.com");
    	        $datacurl = curl_exec($curl);
    	        if (curl_errno($curl)) {
    	        	curl_close($curl);
    				echo "Sorry, server could not connect to rapidshare.com";
    	            exit;
    	        }
            @stream_set_timeout($fp, 300);
            fputs($fp, "POST {$url['path']}  HTTP/1.1\r\n");
            fputs($fp, $head.$vars);
            fflush($fp);
            $buff = 256;
            while (!feof($fp))
            {
                $data = fgets($fp, $buff);
                if($headerdone)
                {
                    print $data;;
                }
                if(!$headerdone)
                {
                    $tmp .= $data;
                    $d = explode("\r\n\r\n", $tmp);
                    if($d[1])
                    {
                        print $d[1];
                        $headerdone = true;
                        $buff = 2048;
                    }
                }
                flush();
                ob_flush();
            }
            @fclose($fp);
            exit;
        }
    PHP:
    Alright, I have uploaded this function to my web server in order for you to test it.

    http://wantpremium.net/download.php

    The whole plan is to fetch a remote file and stream to user/client in other end through my server. For some reason it never goes that far, any idea?

    Thank you for any help! :)
     
    C. Berntsen, Jul 15, 2008 IP