如何从 PHP 发送 GET 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/959063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:28:22  来源:igfitidea点击:

How to send a GET request from PHP?

phphttpget

提问by Veera

I'm planning to use PHP for a simple requirement. I need to download a XML content from a URL, for which I need to send HTTP GET request to that URL.

我打算使用 PHP 来满足一个简单的要求。我需要从 URL 下载 XML 内容,为此我需要向该 URL 发送 HTTP GET 请求。

How do I do it in PHP?

我如何在 PHP 中做到这一点?

回答by Sasha Chedygov

Unless you need more than just the contents of the file, you could use file_get_contents.

除非您需要的不仅仅是文件的内容,否则您可以使用file_get_contents.

$xml = file_get_contents("http://www.example.com/file.xml");

For anything more complex, I'd use cURL.

对于更复杂的事情,我会使用cURL

回答by James Skidmore

For more advanced GET/POST requests, you can install the CURL library (http://us3.php.net/curl):

对于更高级的 GET/POST 请求,您可以安装 CURL 库(http://us3.php.net/curl):

$ch = curl_init("REMOTE XML FILE URL GOES HERE"); // such as http://example.com/example.xml
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);

回答by William Brendel

http_getshould do the trick. The advantages of http_getover file_get_contentsinclude the ability to view HTTP headers, access request details, and control the connection timeout.

http_get应该做的伎俩。http_getover的优点file_get_contents包括能够查看 HTTP 标头、访问请求详细信息以及控制连接超时。

$response = http_get("http://www.example.com/file.xml");

回答by pepe

Remember that if you are using a proxy you need to do a little trick in your php code:

请记住,如果您使用代理,则需要在 php 代码中做一些小技巧:

(PROXY WITHOUT AUTENTICATION EXAMPLE)

<?php
$aContext = array(
    'http' => array(
        'proxy' => 'proxy:8080',
        'request_fulluri' => true,
    ),
);
$cxContext = stream_context_create($aContext);

$sFile = file_get_contents("http://www.google.com", False, $cxContext);

echo $sFile;
?>

回答by Zxaos

Depending on whether your php setup allows fopen on URLs, you could also simply fopen the url with the get arguments in the string (such as http://example.com?variable=value)

根据您的 php 设置是否允许在 URL 上使用 fopen,您也可以简单地使用字符串中的 get 参数来打开 url(例如http://example.com?variable=value

Edit: Re-reading the question I'm not certain whether you're looking to pass variables or not - if you're not you can simply send the fopen request containg http://example.com/filename.xml- feel free to ignore the variable=value part

编辑:重新阅读这个问题我不确定你是否想传递变量 - 如果你不是,你可以简单地发送包含http://example.com/filename.xml的 fopen 请求- 随意忽略变量=值部分

回答by pbreitenbach

I like using fsockopen open for this.

我喜欢为此使用 fsockopen open。

回答by Mostafa

In the other hand, using REST API of other servers are very popular in PHP. Suppose you are looking for a way to redirect some HTTP requests into the other server (for example getting an xml file). Here is a PHP package to help you:

另一方面,使用其他服务器的 REST API 在 PHP 中非常流行。假设您正在寻找一种将某些 HTTP 请求重定向到其他服务器的方法(例如获取 xml 文件)。这是一个可以帮助您的 PHP 包:

https://github.com/romanpitak/PHP-REST-Client

So, getting the xml file:

因此,获取 xml 文件:

$client = new Client('http://example.com');
$request = $client->newRequest('/filename.xml');
$response = $request->getResponse();
echo $response->getParsedResponse();

回答by Mark Baaijens

Guzzle is a very well known library which makes it extremely easy to do all sorts of HTTP calls. See https://github.com/guzzle/guzzle. Install with composer require guzzlehttp/guzzleand run composer install. Now code below is enough for a http get call.

Guzzle 是一个非常有名的库,它使得执行各种 HTTP 调用变得非常容易。请参阅https://github.com/guzzle/guzzle。安装composer require guzzlehttp/guzzle并运行composer install。现在下面的代码足以进行 http get 调用。

$client = new \GuzzleHttp\Client();
$response = $client->get('https://example.com/path/to/resource');

echo $response->getStatusCode();
echo $response->getBody();