2013-02-25 44 views
5

Tôi muốn tải xuống tệp pdf từ this link. Từ trang, chọn tóm tắt hàng tuần, tôi muốn tải xuống bản pdf đầu tiên.Cách tải xuống tệp PDF này bằng cách sử dụng php

PDF Weekly tóm tắt - Tuần trước: Sắp xếp theo người trong cuộc

Tôi đang sử dụng mã thử sau đây để tải về pdf,

<?php 

$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA"; 

if (file_exists($file)) 
{ 
    if (FALSE!== ($handler = fopen($file, 'r'))) 
    { 
     header('Content-Description: File Transfer'); 
     header('Content-Type: application/octet-stream'); 
     header('Content-Disposition: attachment; filename='.basename($file)); 
     header('Content-Transfer-Encoding: chunked'); //changed to chunked 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Pragma: public'); 
     //header('Content-Length: ' . filesize($file)); //Remove 

     //Send the content in chunks 
     while(false !== ($chunk = fread($handler,4096))) 
     { 
      echo $chunk; 
     } 
    } 
    exit; 
} 
echo "<h1>Content error</h1><p>The file does not exist!</p>"; 


?> 

Dường như tôi không thể tải về sử dụng PHP. Tôi có thể tự tải xuống xác nhận cửa sổ bật lên. Bất kỳ đề xuất?

Tôi đã cố gắng cuộn tròn, vẫn không thể tải xuống. Kích thước tệp là số không.

<?php 

    $file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA"; 

    $path = 'c:\download\output.pdf'; 

    $ch = curl_init($file); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $data = curl_exec($ch); 

    curl_close($ch); 

    file_put_contents($path, $data); 


?> 
+4

tại sao không chỉ chỉ khách hàng tại trực tiếp url? nó không giống như bạn đang thực sự thay đổi bất cứ điều gì về pdf - chỉ cần proxy nó. –

+1

curl sẽ là cách thông thường theo thứ tự ngày –

+0

@MarcB, bạn có thể chỉ cho tôi cách thực hiện điều đó không? –

Trả lời

6

Cuối cùng tôi có thể tải xuống tệp PDF với cài đặt curl và CURLOPT_REFERER PHP. Dưới đây là mã.

<?php 

$url = 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA'; 
$path = "/pdf/output.pdf"; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTReportsAccessController?menukey=15.03.00&locale=en_CA'); 

$data = curl_exec($ch); 

curl_close($ch); 

$result = file_put_contents($path, $data); 

    if(!$result){ 
      echo "error"; 
    }else{ 
      echo "success"; 
    } 

>

7

Thay vì nhận được máy chủ PHP của bạn để hoạt động như một proxy tại sao không chỉ:

<?PHP 
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA"; 
header("Location: $file"); 
?> 

Version 2

<?PHP 
$file="https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA"; 
header('Content-type: application/pdf'); 
header('Content-Disposition: attachment; filename="filename.pdf"'); 
readfile($file); 
?> 

Version 3

curl_setopt($ch, CURLOPT_REFERER, 'https://www.sedi.ca/sedi/SVTWeeklySummaryACL?name=W1ALLPDFI&locale=en_CA'); 

Thêm dòng này vào mã ban đầu của bạn?

+0

Tôi chỉ muốn tải xuống bản pdf tại địa phương nhưng không hiển thị nó trên trang web của mình. –

+0

[PHP Bible] (http://www.php.net/manual/en/index.php) – supajason

+0

bạn đã thử mã của mình chưa? nó không làm việc. –

0

Ngoài ra, như đã đề cập ở đây:?

Download a image from SSL using curl?

cho https chúng ta nên bổ sung như sau:

 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
Các vấn đề liên quan