我计划使用PHP来满足一个简单的需求。我需要从一个URL下载XML内容,为此我需要向该URL发送HTTP GET请求。
我如何在PHP中做到这点?
发布于 2009-06-06 05:35:35
除非您需要的不仅仅是文件的内容,否则可以使用file_get_contents
。
$xml = file_get_contents("http://www.example.com/file.xml");
对于更复杂的情况,我会使用cURL。
发布于 2009-06-06 05:36:48
对于更高级的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);
发布于 2009-06-06 05:34:25
http_get
应该可以做到这一点。与file_get_contents
相比,http_get
的优势包括能够查看HTTP头、访问请求详细信息和控制连接超时。
$response = http_get("http://www.example.com/file.xml");
https://stackoverflow.com/questions/959063
复制相似问题