Heim >Backend-Entwicklung >PHP-Tutorial >求帮忙修改个php curl模拟post请求内容后并下载文件解决方案
求帮忙修改个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 />
$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;下载的 x.torrent 用迅雷打开为