Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求指点PHP里的curl_exec问题

求指点PHP里的curl_exec问题

Jun 23, 2016 pm 01:53 PM
php

某网盘的压缩包rar格式外链有referer检测防盗链,本人想把它放到博客上供人下载,于是学习PHP伪造referer,但是得到的结果是一串乱码,估计是把压缩包以文本的形式输出了,本人是菜鸟,求大神指点如何让它以打开原连接的形式下载啊
我的代码如下:

<?php$url = '某个连接地址url.rar格式';$ch = curl_init(); //初始化curl_setopt($ch, CURLOPT_URL, $url); //你要访问的页面curl_setopt($ch, CURLOPT_REFERER, '某个来路url'); //伪造来路页面curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = curl_exec($ch); //执行curl_close($ch); //关闭echo $content;?>
Copy after login


回复讨论(解决方案)

你把这些得到的结果另存成rar格式,如果能打开,说明内容是对的。这样的话,只需要输出一下Header信息就可以了:

header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

可能我刚才写有点问题,如果不行,再试试这种写法

header ( 'Content-type: application/rar' );header ( 'Content-Disposition: attachment; filename="downloaded.rar"' );
Copy after login

你把这些得到的结果另存成rar格式,如果能打开,说明内容是对的。这样的话,只需要输出一下Header信息就可以了:

header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



用这个能下载打开,很感谢!
但是改变了原RAR的文件名称,比如原下载地址的rar名称是 电子书.rar ,但是这个下载的就是download.rar,有什么办法让它保存为原名称吗?


你把这些得到的结果另存成rar格式,如果能打开,说明内容是对的。这样的话,只需要输出一下Header信息就可以了:

header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



用这个能下载打开,很感谢!
但是改变了原RAR的文件名称,比如原下载地址的rar名称是 电子书.rar ,但是这个下载的就是download.rar,有什么办法让它保存为原名称吗?


这个需要你自己分析下载地址的关系



你把这些得到的结果另存成rar格式,如果能打开,说明内容是对的。这样的话,只需要输出一下Header信息就可以了:

header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



用这个能下载打开,很感谢!
但是改变了原RAR的文件名称,比如原下载地址的rar名称是 电子书.rar ,但是这个下载的就是download.rar,有什么办法让它保存为原名称吗?


这个需要你自己分析下载地址的关系




你好,我不大懂php,或许我的这个方法不对。有什么方法伪造来路,然后直接打开原rar地址吗?


你把这些得到的结果另存成rar格式,如果能打开,说明内容是对的。这样的话,只需要输出一下Header信息就可以了:

header("Content-type: application/octet-stream");Header("Content-Disposition: attachment; filename=download.rar" );
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



用这个能下载打开,很感谢!
但是改变了原RAR的文件名称,比如原下载地址的rar名称是 电子书.rar ,但是这个下载的就是download.rar,有什么办法让它保存为原名称吗?


原名称你自己去相应的地方获取,然后写到这里就行了。

$url = '某个连接地址url.rar格式'
这个 url 是你自己给的,如果你都不知道他的文件名是什么,那么别人有如何能知道
如果你知道,那么就把 download.rar 换掉就是了

$url = '某个连接地址url.rar格式';
如果这里的url里有文件名称,你直接提取出来用就行了。

$url = '某个连接地址url.rar格式'
这个 url 是你自己给的,如果你都不知道他的文件名是什么,那么别人有如何能知道
如果你知道,那么就把 download.rar 换掉就是了



那个rar的下载链接是个动态地址,我用了你第一种代码,点击后,下载为download.rar,而且下载过程显示未知大小,感觉我这个思路方法不大对,有好的思路么,只是伪造个来路,然后想直接打开原下载链接

其实

$url = '某个连接地址url.rar格式';$ch = curl_init(); //初始化curl_setopt($ch, CURLOPT_URL, $url); //你要访问的页面curl_setopt($ch, CURLOPT_REFERER, '某个来路url'); //伪造来路页面curl_setopt($ch, CURLOPT_HEADER, 1); curl_exec($ch); //执行
Copy after login
Copy after login
不就行了?

嗯,那样不行,头变成了正文。要这样
既然对方提供的是下载,那么相关信息自然已经都提供了,包括文件名和文件长度

$url = '/某个连接地址url.rar格式';$ch = curl_init(); //初始化curl_setopt($ch, CURLOPT_URL, $url); //你要访问的页面curl_setopt($ch, CURLOPT_REFERER, '某个来路url'); //伪造来路页面curl_setopt($ch, CURLOPT_HEADER, 1); //要读回 http 头curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'func'); //监听返回的数据curl_exec($ch); //执行function func($ch, $str) {  static $s = '@';  if($s) {    $s = trim($str);    if($s) header($s); //如果是头信息,则发送相应的头  }else echo $str; //否则发送数据  return strlen($str);}
Copy after login

其实

$url = '某个连接地址url.rar格式';$ch = curl_init(); //初始化curl_setopt($ch, CURLOPT_URL, $url); //你要访问的页面curl_setopt($ch, CURLOPT_REFERER, '某个来路url'); //伪造来路页面curl_setopt($ch, CURLOPT_HEADER, 1); curl_exec($ch); //执行
Copy after login
Copy after login
不就行了?



老大,这样出来的貌似是以文本形式输出来的,里面乱码一堆哦

对呀,我在 #11 不是已经更正了吗?
比你发现的还早,显然我对自己更负责任些

对呀,我在 #11 不是已经更正了吗?
比你发现的还早,显然我对自己更负责任些



用360和谷歌浏览器正常,
ie高版本的浏览器点击下载,文件名和后缀扩展名乱码了,能下载,但是文件类型都不显示,乱码是什么原因呢

中文文件名出现乱码是正常现象
服务端可能是gbk或utf-8 编码的
客户端也可能是gbk或utf-8 编码的
你需要在收到形如 Content-Disposition: attachment; filename=download.rar 这样的头信息时对其中中文文件名做一下判断,必要时根据客户端的字符集进行转码处理

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles