如何用curl php下载xlsx文件?

时间:2014-07-11 08:14:51

标签: php curl

xls文件:

$accept = fopen("c:\\accept.xls", "wb");

然后:

CURLOPT_FILE   => $accept,

没问题


对于xlsx文件:

使用PHPExcel创建accept.xlsx:

 include "C:\...\Classes\PHPExcel.php";
 include "C:\...\Classes\PHPExcel\Writer\Excel2007.php";
 include "C:\...\Classes\PHPExcel\IOFactory.php";

 $objPHPExcel = new PHPExcel();
 $objPHPExcel->getProperties()->setCreator("my_company");
 $objPHPExcel->getProperties()->setLastModifiedBy("my_company");
 $objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Document");
 $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Document");
 $objPHPExcel->getProperties()->setDescription("Document for Office 2007 XLSX, generated using PHP     classes.");

                          $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
                          $objWriter->save("c:\\accept.xlsx");

创建了文件。  但是如何传递CURLOPT_FILE?

$accept = PHPExcel_IOFactory::load("C:\\accept.xlsx");         // It is not correct.

使用PHPExcel创建文件xlsx。然后

    $accept = fopen("C:\\accept.xlsx", "wb");    // w+ --> such result                                                                                                      

    See in file system (Vista):                                                                                                     
    at first            accept              13:44       10kb                                                                
    then                accept              13:45       0kb                                                             
    then                accept              13:48       292kb       but Don't open                                              



    $accept = fopen("C:\\accept.xlsx", "rb");    //                                                                                                     

    See in file system:                                                                                                     
    at first            accept                          10kb                                                                
    then                                                                                                        

    Warning: curl_setopt_array(): the provided file handle is not writable in                                                                                                       
    … on line 83                        [ curl_setopt_array($ch, $options); ]                                                                               
     cURL Error: SSL certificate problem: unable to get local issuer certificate                                                                                                        



    $accept = fopen("C:\\accept.xlsx", "r+");    //                                                                                                         

    See in file system:                                                                                                     
    at first            accept                          10kb                                                                
    then                accept                          292kb       but Don't open  

因此文件打开,但创建时:10kb。 然后不要打开。


<?php

$b = strrev(mb_substr(sha1(mt_rand() . microtime()),5,16));//
$boundary = "----WebKitFormBoundary". substr_replace($b, mb_strtoupper(mb_substr($b, 7, 5)), 7, 5); 

$headers30 = array
(
    "Host: www.sss.qqq.ua",
    "Connection: keep-alive",
    "Cashe-Control: max-age=0",
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Origin: https://www.sss.qqq.ua",
    "User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
    "Content-Type: multipart/form-data; boundary=".$boundary,
    "Referer: https://www.sss.qqq.ua/shop/price",    
    "Accept-Encoding: gzip,deflate,sdch",
    "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4",
   "Expect:"
);
$data = array
(
    'tp'            =>'xls',
    'tr'            =>'34',
    'ct'            =>'0',
    'dm'            =>'0',
    'cd'            =>'save'
);
function multipart_build_query($fields, $boundary){
  $retval = '';
  foreach($fields as $key => $value){
    $retval .= "--$boundary\r\nContent-Disposition: form-data; name=\"$key\"\r\n\r\n$value\r\n";
  }

  $retval .= "--$boundary--\r\n";
  return $retval;
}
$body = multipart_build_query($data, $boundary);
//
$options30 = array(
CURLOPT_URL             => 'https://www.sss.qqq.ua/shop/price',
CURLOPT_HEADER          => TRUE,       //
CURLOPT_RETURNTRANSFER  => FALSE,      // 
CURLOPT_FOLLOWLOCATION  => TRUE,       // 
CURLOPT_MAXREDIRS       => 7,          //
CURLOPT_ENCODING        => '',         //
CURLOPT_CONNECTTIMEOUT  => 60,         //
CURLOPT_TIMEOUT         => 80,         //
CURLOPT_VERBOSE         => TRUE,       //
CURLOPT_STDERR          => $log30,     //
CURLOPT_WRITEHEADER     => $hd30,      //
CURLOPT_FILE            => $send30,    //
CURLOPT_SSL_VERIFYPEER  => TRUE,       //
CURLOPT_SSL_VERIFYHOST  => 2,          //
CURLOPT_CAINFO          => 'C:/Certificates/mycertspem/manycerts.pem',//
CURLOPT_HTTPHEADER      => $headers30, // 
CURLOPT_COOKIE          => $ssl,       //
CURLOPT_COOKIEFILE      => $cookfile,  //
CURLOPT_COOKIEJAR       => $cookfile,  //
CURLOPT_POST            => TRUE,       //
CURLOPT_POSTFIELDS      => $body       //
);
//------------------------------------------------<
$ch = curl_init();
curl_setopt_array($ch, $options30);
$output_30 = curl_exec($ch);
      if ($output_30 === FALSE) {
       echo "cURL Error: " . curl_error($ch);
      }
$info30 = curl_getinfo($ch);
//sleep(1); // my exp...
curl_close($ch);
fclose($log30);
fclose($hd30);
fclose($send30);//
//------------------------------------------------>
//
?>

0 个答案:

没有答案