• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

求帮忙修改个php curl模拟post请求内容后并下载文件

php 搞代码 4年前 (2022-01-23) 16次浏览 已收录 0个评论
文章目录[隐藏]

下面代码使用curl模拟post请求链接后直接显示出了文件内容,如何修改成不显示内容而直接下载请求到的.torrent格式文件呢

function curl_post($header,$data,$url){ $ch = curl_init(); $res= curl_setopt ($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEE<mark>6来源gaodaimacom搞#^代%!码网</mark><strong>搞gaodaima代码</strong>R, FALSE); curl_setopt ($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); $result = curl_exec ($ch); curl_close($ch); if ($result == NULL) {  return 0; } return $result;}   $url = 'http://www.bttiantang.com/download4.php' ;   $header = array("Host:www.bttiantang.com",  "Content-Type:application/x-www-form-urlencoded",  'User-Agent: Mozilla/4.0 (compatible; MSIE .0; Windows NT 6.1; Trident/4.0; SLCC2;)'); $data = 'action=download&id=26881&uhash=eecf3d3071a7b672c314bc39';$ret = curl_post($header, $data,$url);//需将内容显示修改为文件下载echo $ret;

回复讨论(解决方案)

不要echo $ret;
把$ret写入文件中去。保存起来

不要echo $ret;
把$ret写入文件中去。保存起来

试过使用
header(“Content-Type: application/force-download”);
$ret;
下载的是该PHP空白文件
使用
$filename=$ret;
header(“Content-Type: application/force-download”);
header(“Content-Disposition: attachment; filename=”.basename($filename));
readfile($filename);
则下载一堆乱码的PHP文件

下载的文件名自己定义成.torrent的后缀 这个种子是 5.Flights.Up.2014.LIMITED.720p.BRRip.XviD.AC3-RARBG?

$ret = curl_post($header, $data,$url);//需将内容显示修改为文件下载$filename = 'x.torrent';header("Content-Type: application/force-download");header("Content-Disposition: attachment; filename=$filename");  echo $ret;

下载的 x.torrent 用迅雷打开为

$ret = curl_post($header, $data,$url);//需将内容显示修改为文件下载$filename = 'x.torrent';header("Content-Type: application/force-download");header("Content-Disposition: attachment; filename=$filename");  echo $ret;

下载的 x.torrent 用迅雷打开为

非常感谢xuzuning版主和BingoVictor提供的技术支持,我之前利用A文件获得请求内容后再使用B文件下载A文件并自定义命名和后缀。
而版主提供的方法效率更高,更方便。谢谢了


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:求帮忙修改个php curl模拟post请求内容后并下载文件
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址