2

ダウンロードリンク:

<a href="download.php?words=2000&sort=popular&type=xml">Download php file</a>

download.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Home</title>
</head>
<body>
<h1>PHP file</h1>

<?php
function get_text($text)  
{  
   ....
}      

function get_time($time)  
{  
   ....
}    

$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
$xml = simplexml_load_file($url);
if (count($xml))
{
  foreach($xml->book as $book)
  {
    echo ....      
  }
}
?>

</body>
</html>

download.phpは、ウェブマスターがFTPにアップロードできるようにするための既製のAPIphpスクリプトです。ウェブマスターは、フォームから多くのオプション(例:download.php?words = 2000&sort = Popular&type = xml)を選択し、フォームを送信してカスタムAPIスクリプトを取得できます。

これは、フォームを送信した後にオプションを置き換える行です。

$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];

これは、ダウンロードを強制するためのコードです。しかし、ページ全体を$ content="";で折り返す方法がわかりません。HTMLコードをラップする方法は知っていますが、ページ上のPHP関数とコードをラップする方法はありますか?

header("Content-Disposition: attachment; filename="download.php");
print $content;
4

1 に答える 1

1

それがあなたが望むものであるかどうかはわかりません。ただし、最初のスクリプトを呼び出して出力を取得し、前述のヘッダーを使用して送信する2番目のスクリプトを作成することもできます。

<?php
$words = (int) $_GET['words'];
$sort = $_GET['sort'];

$url = sprintf("http://localhost/wherever/download.php?words=%d&sort=%s&type=xml", $words, $sort);
$content = file_get_contents($url);

header("Content-Disposition: attachment; filename="download.php");
print $content;
?>

そのファイルを「force_download.php」と呼び、<a href="force_download.php?words=2000&sort=popular&type=xml">Download php file</a>代わりにユーザーに呼び出させます。

于 2013-01-31T16:20:32.550 に答える