Please give me some advice on curl_exec in PHP.

Source: Internet
Author: User
Please advise on the curl_exec problem in PHP. This post was last edited by easyrecovery8 from 2014-07-2508: 17: 46. the rar format of a compressed package on a network disk has a referer to detect anti-Leech, I want to put it on my blog for people to download, so I learned 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 to solve the curl_exec problem in PHP.

This post was last edited by easyrecovery8 at 08:17:46

The rar format of a compressed package on an online storage has a referer anti-Leech check. I want to put it on my blog for download. so I learned PHP to forge a referer, 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 kindly advise how to make it download in the form of opening the original connection.
My code is as follows:
 $ Url = 'URL of a specific connection address in url.rar format ';
$ Ch = curl_init (); // Initialization
Curl_setopt ($ ch, CURLOPT_URL, $ url); // The page you want to access
Curl_setopt ($ ch, CURLOPT_REFERER, 'a specific url '); // forged webpage
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );

$ Content = curl_exec ($ ch); // execute
Curl_close ($ ch); // Close
Echo $ content;
?>

------ 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" );

------ Solution --------------------
Reference:
Quote: reference:

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
------ Solution --------------------
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 = '/url.rar format of a connection address ';
$ Ch = curl_init (); // Initialization
Curl_setopt ($ ch, CURLOPT_URL, $ url); // The page you want to access
Curl_setopt ($ ch, CURLOPT_REFERER, 'a specific url '); // forged webpage
Curl_setopt ($ ch, CURLOPT_HEADER, 1); // The http header to be read back.
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 a header information, the corresponding header is sent
} Else echo $ str; // otherwise, send data
Return strlen ($ str );
}


------ Solution --------------------
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.