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

Get contents from URL?

Comments in 'Plugin Development' started by MyNameIsTriXz, Jan 31, 2016.

  1. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    I tried cURL, file_get_contents, readfile but everything returns null, when I debug with broadcastMessage(), it doesnt send anything, and in the console it says " ". How can I get contents from a URL then?

    cURL;
    PHP:
    /* gets the data from a URL */
    public function get_data($url) {
    $ch curl_init();
    $timeout 5;
    curl_setopt($chCURLOPT_URL$url);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_CONNECTTIMEOUT$timeout);
    $data curl_exec($ch);
    curl_close($ch);
    return 
    $data;
    }
    Last edited: Jan 31, 2016
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    post here your code please so we can see where you did errors
  3. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Updated it.
  4. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Use Utils::getURL($url)
    PEMapModder and MyNameIsTriXz like this.
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Oh thx
  6. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    And how do I use it? $player()->sendMessage(Utils::getURL("www.example.com", 10); ?
  7. TheDeibo
    Offline

    TheDeibo Notable Member Plugin Developer

    Joined:
    Sep 8, 2014
    Posts:
    692
    Plugins:
    1
    Minecraft User:
    TheDeibo
  8. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Yeah that's right, $timeout is how long it waits for a response before giving up and returning false.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Technically that is correct. However, the use of Utils::getURL() in the main thread is discouraged, apart from use in onEnable() and onDisable(). The reason is that Utils::getURL() blocks the thread, since it will not run the next line of code until it received a result. Unless this is something from localhost, this usually takes much more than 0.05 second, which is the length of one tick. Any code that takes more than one tick and can be done on other threads should be done on other threads.

Share This Page

Advertisement