There is a file with a bunch of links like "http://www.ex.ua/load/ * *", on which audio files are downloaded (if the link is scored in the browser or used with the wget command). I unsuccessfully try to make a script for uploading these files from the network (the $ links array contains links):

option 1:

foreach ($links as $key => $link) { $fs = fopen($link, 'rb'); $file = fopen('files/'.($key + 1), 'w'); if ($fs && $file) { echo($link.' is downloading.<br/>'); while (!feof($fs)) { fwrite($file, fread($fs, 4096)); } } fclose($file); fclose($fs); } 

option 2:

 foreach ($links as $key => $link) { $c = curl_init($link); $file = fopen('files/'.($key + 1), 'wb'); curl_setopt($c, CURLOPT_FILE, $file); curl_setopt($c, CURLOPT_RETURNTRANSFER, 0); curl_setopt($c, CURLOPT_HEADER, true); curl_exec($c); curl_close($c); fclose($file); } 

option 3:

 foreach ($links as $key => $link) { if (!copy($link, 'files/'.($key + 1))) { echo $link.' copy\'s error<br/>'; } } 

option 4:

 foreach ($links as $key => $link) { $ch = curl_init($link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)'); $output = curl_exec($ch); $fh = fopen('files/'.($key + 1), 'w'); fwrite($fh, $output); fclose($fh); } 

option 5:

 foreach ($links as $key => $link) { file_put_contents('files/'.($key + 1), file_get_contents($link)); } 

And no option does what is needed - does not download the required files. When using option 1, 3, 5, the resulting files contain the html / css code of the page that contains the link to the required file. When using options 2, 4, the resulting files contain

 <html> <head><title>400 Bad Request</title></head> <body bgcolor="white"> <center><h1>400 Bad Request</h1></center> <hr><center>nginx/1.6.2</center> </body> </html> 

So I did not understand how to download files. Maybe someone will tell? :)

UPD.

Screenshots of the request / response when uploading a file by the browser:

http://hkar.ru/xgKC

http://hkar.ru/xgKD

http://hkar.ru/xgKE

Shl. It is impossible to put it in a convenient format (in the form of thumbnails / pictures), therefore I place links to screenshots.

  • I suspect that the page with which you are trying to download uses a redirect to JS. In order for you to give a complete answer, you need to lay out the headers and body of the answer. - flax

1 answer 1

Are you trying to program by simply substituting parts of the program found on Google into source code? Try to read about the functions you use, for example, on php.net

Update

Yandex: wget by mask

Update

A stub page can be sent from the server if the client requesting music is not defined as some kind of browser. When reading byte-by-bye, the header to the server with the music is sent with the identification of your server, I think. See examples of htaccess protection against image theft from the site. In your case, only CURL or some kind of site using a teleport pro can help.

  • Thanks, you really helped :) - LivAlex
  • @alexelev, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). Yandex: wget by mask - Get
  • I run this script on my Windows, I don’t need wget here. And I wonder why NO ONE of the methods used gives the necessary result. Ok, I don’t know much about curl, I’m guessing that I didn’t set any necessary options in set_opt (which is indirectly hinted at as a result - bad request), but what can be wrong when reading byte-reading? - LivAlex
  • What I have depicted in option 4 does not allow the sending server to determine the client? Or is it better to submit a browser description instead of Googlebot? UPD. Changing CURLOPT_USERAGENT leads to the same result ... - LivAlex
  • Yes, look at the headers sent by the browser and set the same values ​​in the CURL settings. If the browser you can get the file, then with the help of CURL everything will turn out. If on that server there is no filtering by hosters ip :) - Get