Search code examples
phpgethttprequestfile-get-contents

Using file_get_contents in php to send a GET request


can I use file_get_contents to send an http request to my host with GET parameters? If yes, I have other 3 questions: Firstly, how? Can I use https instead of http? Can I send them to another host (an external one)? Thx in advance, pls don't blame me if it is stupid


Solution

  • Yes.

    1. Add the parameters to the URL after ?: ?param1=value1&param2=value2. You can use http_build_query() to convert an associative array to URL query parameters.
    2. Yes, just put https: in the URL.
    3. Yes, you can send to any URL.
    $result = file_get_contents('https://www.google.com/search?q=words+to+search+for');