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
Asked
Active
Viewed 428 times
0
-
Sorry, but this does not make any sense at all. What do you want to do with the file contents? Read URL parameters, or full URL, from the file? Or do a file upload (in that case you'd need POST or PUT, not GET). Regarding https vs http: you can always use either, there's nothing specific to GET about the encryption choice. And with http you're sending to an external host anyway, which just can sometimes be the same as the client host. – Hartmut Holzgraefe Sep 07 '22 at 21:12
2 Answers
3
Yes.
- Add the parameters to the URL after
?
:?param1=value1¶m2=value2
. You can usehttp_build_query()
to convert an associative array to URL query parameters. - Yes, just put
https:
in the URL. - Yes, you can send to any URL.
$result = file_get_contents('https://www.google.com/search?q=words+to+search+for');

Barmar
- 741,623
- 53
- 500
- 612
-
-
Only if you want to do something with the value returned. My example is a Google search, it's not very useful if you don't use the result. `GET` requests are not usually used for side effects. – Barmar Sep 07 '22 at 22:01
-
I need it to set txt files in the host, don't need to use immediately the result – M1001 Sep 07 '22 at 22:06
-
What do you mean by "set txt files"? If you're trying to upload, that's usually done with `POST`. – Barmar Sep 07 '22 at 22:07
-
Not upload, just use fwrite() to write in files, I set the function in the other host and pass the parameters via https request – M1001 Sep 07 '22 at 22:12
-
-
You can use the result directly. `fwrite($fp, file_get_contents(...))`. It's essentially the same thing. – Barmar Sep 07 '22 at 22:12
-
-
Now I understand. THe server for the GET request writes to a file. Like I said, that's unusual. Conventionally, GET requests should be idempotent, because browsers cache them. – Barmar Sep 07 '22 at 22:20
-
These are stupid data, so it is not the problem, just another question, the host cache them? I mean, need I to clear the cache if I send a lot of data to don't slow down the host? – M1001 Sep 07 '22 at 22:22
-
If you have time, can you also tell me how to do it via POST? So i value what is better, maybe in another answer – M1001 Sep 07 '22 at 22:24
-
Hosts don't cache, but browsers do. Web application design is based on browsers. – Barmar Sep 07 '22 at 22:24
-
I would use `curl` to do it with `POST`. But if you want to use `file_get_contents()`, see the other answer, which shows how to create a context that specifies the method, body, etc. – Barmar Sep 07 '22 at 22:25
-
What happens if the browser cache them? What are the problems that will happen? – M1001 Sep 07 '22 at 22:27
-
-
If the browser caches it, the request is never sent to the server, so the file isn't updated. – Barmar Sep 07 '22 at 22:30
-
So, If i make it with POST, all will work as I need to send informations and upload the files? – M1001 Sep 07 '22 at 22:32
-
1
-
Ok, very last thing, can you tell me how to do it with POST pls? The other one modified the answer, and I also don't know if it was complete – M1001 Sep 07 '22 at 22:39
-
There are many tutorials on how to send POST requests. I'm not going to do that. – Barmar Sep 07 '22 at 22:40
-
1https://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents – Barmar Sep 07 '22 at 22:40
1
file_get_contents is a stream fonction, so you can create a stream context and pass it to this function :
$context = stream_context_create(
array( 'https' => // or any other protocol
array(
'method' => 'GET', // or post ..
// any other params you need
)
)
$response = file_get_contents('https://my-api.com/users' . http_build_query($params), false, $context);

soma
- 176
- 9
-
1`GET` requests don't have contents, so there's little point in sending `Content-type:` – Barmar Sep 07 '22 at 22:02
-
Why need I to set the context? I send the GET parameters putting them in the url. Is it also useless to set the content type, no? – M1001 Sep 07 '22 at 22:04
-
-
-
contexts gives full control over context fonctions, i thought you want to sit some specific params, if it's not the case just go with $response = file_get_contents('https://my-api.com/users' . http_build_query([$params])); and it should work – soma Sep 07 '22 at 22:14