简体   繁体   English

使用 php 中的 file_get_contents 发送 GET 请求

[英]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?我可以使用 file_get_contents 使用 GET 参数向我的主机发送 http 请求吗? If yes, I have other 3 questions: Firstly, how?如果是,我还有其他 3 个问题:首先,如何? Can I use https instead of http?我可以使用 https 代替 http 吗? Can I send them to another host (an external one)?我可以将它们发送到另一台主机(外部主机)吗? Thx in advance, pls don't blame me if it is stupid提前谢谢,如果它很愚蠢,请不要怪我

Yes.是的。

  1. Add the parameters to the URL after ?将参数添加到 URL 之后? : ?param1=value1&param2=value2 . : ?param1=value1&param2=value2 You can use http_build_query() to convert an associative array to URL query parameters.您可以使用http_build_query()将关联数组转换为 URL 查询参数。
  2. Yes, just put https: in the URL.是的,只需将https:放入 URL 中即可。
  3. Yes, you can send to any URL.是的,您可以发送到任何 URL。
$result = file_get_contents('https://www.google.com/search?q=words+to+search+for');

file_get_contents is a stream fonction, so you can create a stream context and pass it to this function: file_get_contents 是一个 stream 函数,因此您可以创建一个 stream 上下文并将其传递给此 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM