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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles