开发者

Fetching a file from a PHP file in Google App Engine

开发者 https://www.devze.com 2023-04-10 06:43 出处:网络
I need to know if there is anyway that I can use file_get_html or any equivalent function in php on GAE? I know it has something called URLFetch() but I am not able to understand how I will call that

I need to know if there is anyway that I can use file_get_html or any equivalent function in php on GAE? I know it has something called URLFetch() but I am not able to understand how I will call that from a php fi开发者_如何学Pythonle.

Any help?


You cannot run PHP on Google App Engine. You can create a servlet which will read from any given URL and manipulate the data in any way you would need to, in Java (since you tagged this question with the Java tag).

From the AppEngine URL Fetch Java API Overview:

URL url = new URL("http://www.example.com/atom.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;

while ((line = reader.readLine()) != null) {
    // ...
}
reader.close();

If you meant that you are running PHP in another application and you wish to call your AppEngine servlet from said PHP application, then you can map the servlet which performs this URL fetch to a URL within your AppEngine application, then hit that URL from your PHP application. This, however, seems like a bad design, as you're making two network calls when you could have just used done it within the PHP application in the first place.


Here's a quick and dirty wrapper function I created for URL Fetch using PHP via Quercus on Google App Engine:

function fetch_url($url){

    import java.net.URL;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    $java_url = new URL($url);
    $java_bufferreader = new BufferedReader(new InputStreamReader($java_url->openStream()));

    while (($line = $java_bufferreader->readLine()) != null) {
        $content .= $line;
    }

    return $content;
}

// Sample usage:
echo fetch_url('http://google.com');

Hope this helps someone who is as lost as I was.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号