Sas*_*gov 358
除非您需要的不仅仅是文件的内容,否则您可以使用file_get_contents.
$xml = file_get_contents("http://www.example.com/file.xml");
Run Code Online (Sandbox Code Playgroud)
对于任何更复杂的东西,我都会使用cURL.
Jam*_*ore 130
对于更高级的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);
Run Code Online (Sandbox Code Playgroud)
Wil*_*del 63
http_get应该做的伎俩.http_getover 的优点file_get_contents包括查看HTTP标头,访问请求详细信息和控制连接超时的功能.
$response = http_get("http://www.example.com/file.xml");
Run Code Online (Sandbox Code Playgroud)
小智 20
请记住,如果您使用代理,则需要在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;
?>
Run Code Online (Sandbox Code Playgroud)
根据您的php设置是否允许对URL进行fopen,您还可以使用字符串中的get参数(例如http://example.com?variable=value)简单地打开url
编辑:重新阅读问题我不确定你是否想要传递变量 - 如果你不是,你可以简单地发送包含http://example.com/filename.xml的fopen请求- 随意忽略变量=值部分
小智 7
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();
Run Code Online (Sandbox Code Playgroud)
另一方面,使用其他服务器的REST API在PHP中非常流行.假设您正在寻找一种方法将一些HTTP请求重定向到另一台服务器(例如获取xml文件).这是一个PHP包,可以帮助您:
https://github.com/romanpitak/PHP-REST-Client
Run Code Online (Sandbox Code Playgroud)
所以,获取xml文件:
$client = new Client('http://example.com');
$request = $client->newRequest('/filename.xml');
$response = $request->getResponse();
echo $response->getParsedResponse();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
491542 次 |
| 最近记录: |