I am requesting a website like (Rapidshare) that in response gives me either the requested file or some HTML telling that I need to wait xx seconds before I download file.
at
I am confused how to code this scenario.
Now if I do posting using an HTML form, setting some fields in it and then auto submitting it using Javascript to the target site. If it is success, user is prompted with if he wants to download that fiel, and if it is fail for some reason like if user need to wait, it takes user to THAT site URL.
to avoid user redirecting to target site error page. I am using CURL, that sends POST request and reads response.
Now, problem is, if reponse of CURL request is a file, page keeps loadin until and unless whole file recieved in response is download on server and stored in that variable.
this function That makes curl request at $url with $headerz as header values, and $encoded are request parameters
function makeCurlPostRequest($url, $headerz, $encoded){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if($headerz!=null)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerz);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
if($encoded!=null)
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_AUTOREFERER, 0);
curl_setopt($ch, CURLOPT_REFERER, $url);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function checkUrl($file,$url,$headerz){
$post_data = array ( "premium_acc" => "checked",
"linkx" => "$file"
);
$encoded = '';
foreach($post_data as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
$scrapHTML = makeCurlPostRequest($url, $headerz, $encoded);
return $scrapHTML;
}
CheckUrl, takes 3 parameters, $file link of file to download, $url url to request at and $headerz are header values.
Now I uploaded a test file
a.txt
with contents
ABCDABCDABCD
Then upon making a successful request and printing responce on Web browser using ti outputs this
HTTP/1.1 200 OK Date: Fri, 08 Jul 2011
07:25:43 GMT Server: Apache/2.2.16
(Debian) X-Powered-By:
PHP/5.3.3-7+squeeze1 Expires: Mon, 26
Jul 1997 05:00:00 GMT Last-Modified:
Fri, 08 Jul 2011 07:25:43GMT
Cache-Control: no-cache,
must-revalidate Pragma: no-cache
Content-Disposition: filename=”a.txt”
Connection: close Accept-Ranges: bytes
Content-Length: 1 Content-Type:
application/force-download
ABCDABCDABCD
Where ABCDABCDABCD is contents ot TXT file. if it is an audio file it outputs some random characters after downloading it completely.
I need to prompt this data to user to download instead of first saving it on server or in a variable.
is there a way, I can check responce header if returned is a file and if a file then prompt user to download.
any solution by this CURL method?
OR
<form action="d.php" method="POST">
<input type="hidden" name ="url" value="<?php echo $url;?>"/>
<?php
foreach($scrapHTML->find('input') as $field) {
$name = $field->name;
$value = $field->value;
?>
<input type="hidden" name ="<?php echo $name;?>" value="<?php echo $value;?>"/>
<?php
}
?>
</form>
<script type="text/javascript">
void(document.forms[0].submit());
</script>
This HTML form sends request to target URL and on success prompts user with file to download but problem is in case of error it takes user to THAT website error or waiting page that I don’t want to be visible to user.
Please provide a solution, either using curl or using HTML form post.
regards,
aqif
EDIT : Set this to send POST Variables