Next Header

Discussion in 'PHP' started by SNaRe, Jan 5, 2008.

  1. #1
    I'm working on getting download url of dailymotion videos.
    <?php
    
    $url='http://www.dailymotion.com/related/6392044/video/x3wt8i_a-heartfelt-new-years-greeting_blog';
    $agent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $buffer = curl_exec($ch);
    $curl_info = curl_getinfo($ch);
    curl_close($ch);
    print_r($curl_info);
    ?>
    
    PHP:
    I checked with a http live header program that when i enter to site i shows download link in header. I'm trying to reach it with php i wrote the code above but i tokk this result
    
    Array
    (
        [url] => http://www.dailymotion.com/related/6392044/video/x3wt8i_a-heartfelt-new-years-greeting_blog
        [content_type] => text/html; charset=utf-8
        [http_code] => 200
        [header_size] => 546
        [request_size] => 226
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0.942
        [namelookup_time] => 0
        [connect_time] => 0.002
        [pretransfer_time] => 0.002
        [size_upload] => 0
        [size_download] => 66469
        [speed_download] => 70561
        [speed_upload] => 0
        [download_content_length] => 0
        [upload_content_length] => 0
        [starttransfer_time] => 0.923
        [redirect_time] => 0
    )
    
    
    Code (markup):
    So I think i need to get the step where the video starts to play.
    Can someone help me to solve this problem?
     
    SNaRe, Jan 5, 2008 IP
  2. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #2
    Maybe this can help us more ?
    
    <?php
    //PHP5
    class Curl_example {
    	private $request;
    	private $response;
    	private $response_meta_info;
            private $url;
    
    	function __construct($request, $url) {
    		//E.g.
    		//$request['report_num']=1432;
    		//$request['token']='some string'
    		$this->request = $request;
                    $this->url = $url;
    	}
    
    	function get() {
    		//initiate curl transfer
    		$ch = curl_init();
    
    		//set the URL to connect to
    		curl_setopt($ch, CURLOPT_URL, $this->url);
    
    		//do not include headers in the response
    		curl_setopt($ch, CURLOPT_HEADER, 0);
    
    		//register a callback function which will process the headers
    		//this assumes your code is into a class method, and uses $this->readHeader as the callback //function
    		curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
    
    		//Some servers (like Lighttpd) will not process the curl request without this header and will return error code 417 instead. 
    		//Apache does not need it, but it is safe to use it there as well.
    		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
    
    		//Response will be read in chunks of 64000 bytes
    		curl_setopt($ch, CURLOPT_BUFFERSIZE, 64000);
    
    		//Tell curl to use POST
    		curl_setopt($ch, CURLOPT_POST, 1);
    
    		//Tell curl to write the response to a variable
    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    		//Register the data to be submitted via POST
    		curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request);
    
    		//Execute request
    		$this->response = curl_exec($ch);
    
    		//get the default response headers
    		$headers = curl_getinfo($ch);
    
    		//add the headers from the custom headers callback function
    		$this->response_meta_info = array_merge($headers, $this->response_meta_info);
    
    		//close connection
    		curl_close($ch);
    
    		//catch the case where no response is actually returned
    		//but curl_exec returns true (on no data) or false (if cannot connect)
    		if (is_bool($this->response)) {
    			if ($this->response==false){
    				throw new Exception('No connection');
    			} else {
    				//null the response, because there are actually no data
    				$this->response=null;
    			}
    
    		}
    		return $this->response;
    	}
    
    	/**
    	 * CURL callback function for reading and processing headers
    	 * Override this for your needs
    	 * 
    	 * @param object $ch
    	 * @param string $header
    	 * @return integer
    	 */
    	private function readHeader($ch, $header) {
    		//extracting example data: filename from header field Content-Disposition
    		$filename = $this->extractCustomHeader('Content-Disposition: attachment; filename=', '\n', $header);
    		if ($filename) {
    			$this->response_meta_info['content_disposition'] = trim($filename);
    		}
    		return strlen($header);
    	}
    
    	private function extractCustomHeader($start,$end,$header) {
    		$pattern = '/'. $start .'(.*?)'. $end .'/';
    		if (preg_match($pattern, $header, $result)) {
    			return $result[1];
    		} else {
    			return false;
    		}
    	}
    	
    	function getHeaders() {
    		return $this->response_meta_info;
    	}
    }
    
    
    $c = new Curl_example(array('report'=>213,'token'=>'some string'), 'http://someurl);
    
    $data = $c->get();
    
    $headers = $c->getHeaders();
    
    ?>
    
    [Edit section] Conclusion
    PHP:
     
    SNaRe, Jan 5, 2008 IP
  3. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #3
    Barti1987, Jan 6, 2008 IP