Please give me some advice on the curl_exec problem in PHP. the rar-format external chain of a network disk has a referer anti-Leech check. I want to put it on my blog for download, so I learned how to forge a referer in PHP, but the result is a garbled string. it is estimated that the compressed package is output in the form of text. I am a newbie. please advise me how to make it download in the form of opening the original connection.
My code is as follows:
Reply to discussion (solution)
You save the obtained results in the rar format. If yes, the content is correct. In this case, you only need to output the Header information:
header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Maybe I just wrote something wrong. if not, try again.
header ( 'Content-type: application/rar' );header ( 'Content-Disposition: attachment; filename="downloaded.rar"' );
You save the obtained results in the rar format. If yes, the content is correct. In this case, you only need to output the Header information:
header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Thanks for downloading it!
But I changed the name of the original RAR file. for example, the original rar name is e-book .rar, but this is the downloaded character download.rar. Is there any way to save it as the original name?
You save the obtained results in the rar format. If yes, the content is correct. In this case, you only need to output the Header information:
header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Thanks for downloading it!
But I changed the name of the original RAR file. for example, the original rar name is e-book .rar, but this is the downloaded character download.rar. Is there any way to save it as the original name?
This relationship needs to be analyzed by yourself
You save the obtained results in the rar format. If yes, the content is correct. In this case, you only need to output the Header information:
header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Thanks for downloading it!
But I changed the name of the original RAR file. for example, the original rar name is e-book .rar, but this is the downloaded character download.rar. Is there any way to save it as the original name?
This relationship needs to be analyzed by yourself
Hello, I don't know much about php. maybe this method is incorrect. Is there any way to forge the path and then directly open the original rar address?
You save the obtained results in the rar format. If yes, the content is correct. In this case, you only need to output the Header information:
header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Thanks for downloading it!
But I changed the name of the original RAR file. for example, the original rar name is e-book .rar, but this is the downloaded character download.rar. Is there any way to save it as the original name?
You can get the original name by yourself and write it here.
$ Url = 'format of a connection address in url.rar'
This url is provided by you. if you don't know what the file name is, how can someone else know it?
If you know, replace download.rar.
$ Url = 'URL of a specific connection address in url.rar format ';
If the url contains a file name, you can extract it directly.
$ Url = 'format of a connection address in url.rar'
This url is provided by you. if you don't know what the file name is, how can someone else know it?
If you know, replace download.rar.
The download link of the rarn is a dynamic address. I use your first code. after clicking it, download it as download.rar, and the download process shows the unknown size. I feel that this method is not very good. do you have a good idea? just forge a path and try to open the original download link
Actually
$ Url = 'URL of a specific connection address in url.rar format '; $ ch = curl_init (); // initialize curl_setopt ($ ch, CURLOPT_URL, $ url); // The page you want to access curl_setopt ($ ch, CURLOPT_REFERER, 'a specific url '); // counterfeit the Origin page curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_exec ($ ch); // execute
No?
Well, that's not the case. the header becomes the body. To do this
Since the other party provides download, the relevant information is provided, including the file name and file length.
$ Url = '/A connection address in url.rar format'; $ ch = curl_init (); // initialize curl_setopt ($ ch, CURLOPT_URL, $ url ); // The page you want to access curl_setopt ($ ch, CURLOPT_REFERER, 'a specific url '); // counterfeit the url curl_setopt ($ ch, CURLOPT_HEADER, 1 ); // read the http header curl_setopt ($ ch, CURLOPT_WRITEFUNCTION, 'func'); // listen to the returned data curl_exec ($ ch); // execute function func ($ ch, $ str) {static $ s = '@'; if ($ s) {$ s = trim ($ str); if ($ s) header ($ s ); // if it is the header information, send the corresponding header} else echo $ str; // otherwise, send the data return strlen ($ str );}
Actually
$ Url = 'URL of a specific connection address in url.rar format '; $ ch = curl_init (); // initialize curl_setopt ($ ch, CURLOPT_URL, $ url); // The page you want to access curl_setopt ($ ch, CURLOPT_REFERER, 'a specific url '); // counterfeit the Origin page curl_setopt ($ ch, CURLOPT_HEADER, 1); curl_exec ($ ch); // execute
No?
Boss, it seems to be output in the form of text, with a bunch of garbled characters in it.
Yes, I have corrected it in #11?
It's earlier than what you found. Obviously, I am more responsible for myself.
Yes, I have corrected it in #11?
It's earlier than what you found. Obviously, I am more responsible for myself.
Using 360 and Google Chrome is normal,
In the browser of higher ie versions, click to download. the file name and suffix extension are garbled and can be downloaded, but the file type is not displayed. what is the cause of garbled characters?
It is normal that Chinese file names are garbled
The server may be gbk or UTF-8 encoded
The client may also be gbk or UTF-8 encoded
When you receive header information such as Content-Disposition: attachment; filename=download.rar, make a judgment on the Chinese file name. if necessary, transcode the file according to the character set of the client.