English 中文(简体)
从另一个PHP脚本下载由PHP生成的页面内容。
原标题:
  • 时间:2009-05-12 18:59:26
  •  标签:

I have a PHP script on a server that generates the XML data on the fly, say with Content-Disposition:attachment or with simple echo, doesn t matter. I ll name this file www.something.com/myOwnScript.php

在另一个服务器上,在另一个 PHP 脚本中,我希望能够获取此文件(以避免“将文件保存到磁盘”)作为字符串(使用路径 www.something.com/myOwnScript.php),然后操作脚本生成的 XML 数据。

Is this possible without using web services? security implications?

Thanks

最佳回答

简单的答案是,是的。

$output = file_get_contents( http://www.something.com/myOwnScript.php );

echo  <pre> ;
print_r($output);
echo  </pre> ;
问题回答

If you want more control over how you request the data (spoof headers, send post fields etc.) you should look into cURL. link text

如果你使用的是共享主机,你可能无法使用file_get_contents。这主要是因为它是与包含远程文件相同的权限集的一部分。无论如何...

If you re stuck in that circumstance, you might be able to use CURL:

<?php
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "example.com");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);     
?>

It is more code, but it s still simple. You have the added benefit of being able to post data, set headers, cookies... anything you could do with a highly configurable browser. This makes it useful when people attempt to block bots.





相关问题
热门标签