The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Delphi Programming
|
PHP to Delphi or something..
Discuss PHP to Delphi or something.. in the Delphi Programming forum on Dev Shed. PHP to Delphi or something.. Delphi Programming forum discussing Delphi related topics including Kylix, C++ Builder, and more. Delphi is a high-performance language, originally based on the PASCAL language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 7th, 2012, 03:35 PM
|
|
Registered User
|
|
Join Date: Nov 2007
Posts: 5
Time spent in forums: 5 h 28 m 48 sec
Reputation Power: 0
|
|
|
PHP to Delphi or something..
OK, I'd like to be able to get the following code to run on my computer rather than my server.
It's code to download a youtube clip and save it as an flv file and, on my server, it works brilliantly. However, I'd like to somehow include it as a feature for my media player so that users can do the same but on their own P.C.
Any ideas? Can this be converted to Delphi 7?
Code:
<?php
class youtubegrabber{
var $youtube_video_url;
var $test;
var $final_flv_filename;
var $cookies_path;
var $curl_headers;
var $flv_url;
function __construct($youtube_video_url, $final_flv_filename, $test = 0){
$this->youtube_video_url = $youtube_video_url;
$this->test = $test;
$this->final_flv_filename = $final_flv_filename;
$this->youtube_video_id = $this->get_youtube_video_id();
$this->cookies_path = "cookies.txt";
$clear_cookies = $this->clear_cookies();
$this->curl_headers = array(
"Accept-Language: en-us",
"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15",
"Connection: Keep-Alive",
"Cache-Control: no-cache"
);
$this->flv_url = $this->get_flv_url();
$save_binary = $this->get_curl_binary();
$clear_cookies = $this->clear_cookies();
}
function get_youtube_video_id(){
$thearray = explode("watch?v=", $this->youtube_video_url);
return $thearray[1];
}
function clear_cookies(){
if(file_exists($this->cookies_path)){
unlink($this->cookies_path);
}
$ourFileName = $this->cookies_path;
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
}
function get_flv_url(){
$html = $this->curl_get_url($this->youtube_video_url);
preg_match_all("/var.*?swf.*?=.*?\"(.*?)watch-player.*?innerHTML.*?=.*?swf/is", $html, $matches);
$decoded = urldecode($matches[1][0]);
preg_match_all("/url=(.*?)\,/is", $decoded, $matches);
$matches = $matches[1];
for($i = 0; $i < count($matches); $i++){
$test = explode("&", $matches[$i]);
$matches[$i] = $test[0];
$matches[$i] = urldecode($matches[$i]);
}
$final_flv_url = "";
foreach($matches AS $this_url){
$headers = $this->curl_get_headers($this_url);
$headers = split("\n", trim($headers));
foreach($headers as $line) {
if (strtok($line, ':') == 'Content-Type') {
$parts = explode(":", $line);
$content_type = strtolower(trim($parts[1]));
if ( $this->contains("video/x-flv", $content_type) ){
$final_flv_url = $this_url;
return $final_flv_url;
}
}
}
}
return false;
}
function curl_get_url($url){
$cookie_path = $this->cookies_path;
$headers = $this->curl_headers;
$ch = curl_init();
//$referer = 'http://www.google.com/search';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // this pretends this scraper to be browser client IE6 on Windows XP, of course you can pretend to be other browsers just you have to know the correct headers
//curl_setopt($get, CURLOPT_REFERER, $referer); // lie to the server that we are some visitor who arrived here through google search
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// set user agent
//curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13");
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
return $output;
}
function curl_get_headers($url){
$cookie_path = $this->cookies_path;
$headers = $this->curl_headers;
$ch = curl_init();
//$referer = 'http://www.google.com/search';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );
$results = curl_exec($ch);
return $results;
}
function get_curl_binary(){
$url = $this->flv_url;
$cookie_path = $this->cookies_path;
$headers = $this->curl_headers;
$final_flv_filename = $this->final_flv_filename;
$ch = curl_init($url);
$fp = fopen($final_flv_filename, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // this pretends this scraper to be browser client IE6 on Windows XP, of course you can pretend to be other browsers just you have to know the correct headers
curl_exec($ch);
curl_close($ch);
fclose($fp);
}
function contains($substring, $string) {
$pos = strpos($string, $substring);
if($pos === false) {
// string needle NOT found in haystack
return false;
}
else {
// string needle found in haystack
return true;
}
}
}
//$url = "http://www.youtube.com/watch?v=XZxo7IznQnk";
//$filename = "test.flv";
//$youtubegrabber = new youtubegrabber($url, $filename, 0);
//echo "Should be done.<br>";
?>
|

June 7th, 2012, 04:56 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: Carlsbad, CA
|
|
|
I am sure it could be written in Delphi by anyone with the appropriate knowledge of the
youtube API.
I don't think I would recommend simply trying to translate the code you have.
In Delphi you would want to create a class or classes to do the job.
Have you done any searches to see whether anyone has already written such a component?
|

June 20th, 2012, 12:09 AM
|
|
Contributing User
|
|
Join Date: May 2012
Posts: 141
Time spent in forums: 1 Day 5 h 1 m 56 sec
Reputation Power: 2
|
|
I don't know this for sure, for haven't done this. But from the code, I believe it can be done with Delphi 7 using either: - TIdHttp from Indy library (usually already installed with Delphi 7)
- THttpSend from Synapse library (google for "synapse ararat")
- Using instance of IXmlHttpRequest (part of MsXml)
- Using ICS library
All you need to do is setting cookies and headers for them to work with. And the needed cookies and headers is already there in your php code.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|