Download contents of the PHP generated page from another PHP script

可紊 提交于 2019-12-11 04:54:44

问题


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

On another server, in another PHP script I want to be able to get this file (to avoid "saving file to disk") as a string (using the path www.something.com/myOwnScript.php) and then manipulate XML data that the script generates.

Is this possible without using web services? security implications?

Thanks


回答1:


Simple answer, yes:

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

echo '<pre>';
print_r($output);
echo '</pre>';



回答2:


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




回答3:


If you're on a shared host, you might find that you cannot use file_get_contents. This mainly because it is part of the same permission sets that allow you to include remote files. Anyway...

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.



来源:https://stackoverflow.com/questions/854302/download-contents-of-the-php-generated-page-from-another-php-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!