Home > Backend Development > PHP Tutorial > 求帮忙修改个php curl模拟post请求内容后并下载文件解决方案

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:24:54
Original
886 people have browsed it

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

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

------解决思路----------------------
不要echo $ret;
把$ret写入文件中去。保存起来
------解决思路----------------------
$ret = curl_post($header, $data,$url);<br /><br />//需将内容显示修改为文件下载<br />$filename = 'x.torrent';<br />header("Content-Type: application/force-download");<br />header("Content-Disposition: attachment; filename=$filename");  <br /><br />echo $ret;
Copy after login
下载的 x.torrent 用迅雷打开为

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template