Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved get contents from a web page on andorid

Comments in 'Plugin Development' started by Svile, Dec 13, 2015.

  1. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    fopen(), file_get_contents() or curl aren't working if the server is running on an android device.
    example:
    PHP:
    $link 'http://example.com/page.php';

    //using fopen
    $fopen = @fread(@fopen($link'r'), 1024);
    echo 
    $fopen;  // ''

    //using curl
    $utils = new pocketmine\utils\Utils;
    echo (@
    $utils->getURL($link));  // ''

    //using file_get_contents
    echo (@file_get_contents($link));  //  ''
    Can someone help me, please ?
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Utils->getURL is a static method. Maybe the webpage doesn't exist? And instead of doing:
    PHP:
    echo(@file_get_contents($link));
    Try:
    PHP:
    echo file_get_contents($link);
    Svile likes this.
  3. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    Utils::getURL ... sorry . it works using pocketmine PC , now i'm going to try without @
    Last edited: Dec 13, 2015
  4. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    Fatal error: Uncaught exception 'RuntimeException' with message 'file_get_contents(): php_network_getaddresses: gethostbyname failed. errno=2'

    ...but on PC is working
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Are you sure the server is connected to the Internet?
    Utils::getURL() is better.
  6. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    i'm sure , players are inside...
  7. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    If it's not too much trouble , can someone try Utils::getURL() on android ? thanks a lot :)
  8. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    try this function
    PHP:
    private function url_get_contents($url) {
            if(
    function_exists('curl_exec')){
                
    $conn curl_init($url);
                
    curl_setopt($connCURLOPT_SSL_VERIFYPEERtrue);
                
    curl_setopt($connCURLOPT_FRESH_CONNECT,  true);
                
    curl_setopt($connCURLOPT_RETURNTRANSFER1);
                
    $url_get_contents_data = (curl_exec($conn));
                
    curl_close($conn);
            }elseif(
    function_exists('file_get_contents'))
                
    $url_get_contents_data file_get_contents($url);
            elseif(
    function_exists('fopen') && function_exists('stream_get_contents')){
                
    $handle fopen ($url"r");
               
    $url_get_contents_data stream_get_contents($handle);
            }else
                
    $url_get_contents_data false;
            return 
    $url_get_contents_data;
        } 
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    cURL must exist in PocketMine. It is checked in PocketMine.php
  10. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    i will try...btw , file_get_contents() and fopen() will not work anyway
  11. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    i tryed on another android phone and is working well. so it's my phone , and i don't understand why :(
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Break the lines. You're making it as if the static modifier made the page not found :p
    Nope, the HTTP server has to return some content (including content of zero length) even if the page doesn't exist.


    You mean Utils::getURL() doesn't work?
    Gamecrafter likes this.
  13. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    yes but only on my phone
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Error messages? Return value?
  15. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    PHP:
    $this->getLogger()->info(print_r(Utils::getURL($link)));
    print 1 (but should be a string) in the console without errors
  16. Svile
    Offline

    Svile Active Member

    Joined:
    Mar 30, 2015
    Posts:
    239
    Minecraft User:
    svile
    PHP:
    $this->getLogger()->info(print_r(file_get_contents($link)));
    doesn't return anything and cause this error:
    Code:
    RuntimeException: "file_get_contents(): php_network_getaddresses: gethostbyname failed. errno=2" (E_WARNING)
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Try doing var_dump() instead? That function should only return a string or Boolean false.

Share This Page

Advertisement