php curl保存下载文件

时间:2014-06-10 08:59:37

标签: php curl

我将此保存为本地的php文件,这在浏览器中正常工作

<form name="Form" method="post" action="http://www.idx.co.id/en-us/home/listedcompanies/issuedhistory.aspx" id="Form" enctype="multipart/form-data"><div><input type="hidden" name="StylesheetManager_TSSM" id="StylesheetManager_TSSM" value="" /><input type="hidden" name="ScriptManager_TSM" id="ScriptManager_TSM" value="" /><input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="dnn$ctr951$MainView$lbDownload" /><input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" /></div></form>
<script>
document.getElementById("Form").submit();
</script>

但如何在卷曲中这样做? 我试过了

function download($url, $path)
    {
      $fp = fopen ($path, 'w');
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, $url );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
      curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
      curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 30 );
      curl_setopt( $ch, CURLOPT_FILE, $fp );
      $params = "StylesheetManager_TSSM=&ScriptManager_TSM=&__EVENTTARGET=dnn$ctr951$MainView$lbDownload&__EVENTARGUMENT=";
      curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
      curl_exec( $ch );
      curl_close( $ch );
      fclose( $fp );

      if (filesize($path) > 0) return true;
    }

    $url = 'http://www.idx.co.id/en-us/home/listedcompanies/issuedhistory.aspx';
    $path = 'corpactions.csv';

    if (download($url, $path)){
        echo 'Download complete!'; 
    }

但它没有按预期工作,它下载了另一页

1 个答案:

答案 0 :(得分:1)

尝试更改此

$params = "StylesheetManager_TSSM=&ScriptManager_TSM=&__EVENTTARGET=dnn$ctr951$MainView$lbDownload&__EVENTARGUMENT=";

进入这个:

$params = 'StylesheetManager_TSSM=&ScriptManager_TSM=&__EVENTTARGET=dnn$ctr951$MainView$lbDownload&__EVENTARGUMENT=';

问题可能是$ czt951将被解析为php var。

更多细节请阅读本答案: https://stackoverflow.com/a/3446286/2441442

相关问题