使用curl PHP获取gzip压缩的XML文件时出现问题 [英] Problems getting gzipped XML files with curl PHP

查看:314
本文介绍了使用curl PHP获取gzip压缩的XML文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从curl的xml.gz文件抓取数据。我可以下载文件,但无法获得可用的XML与我的任何尝试。当我尝试打印XML时,我得到一个长列表的乱码特殊字符,如:

I'm trying to grab data from an xml.gz file with curl. I'm able to download the file, but can't get the usable XML with any of my attempts. When I try to print the XML, I'm getting a long list of garbled special characters such as:

‹ì½ûrâÈ–7ú?E~{Çž¨Ši°î—Ù5=ÁÍ6]`Ø€ë²ãDLÈ u

一个简单的方法只是解压缩和编码这个XML?可能通过SimpleXML?文件很大,需要进行身份验证。这是我目前的代码:

Is there a simple way to just uncompress and encode this xml? Possibly through SimpleXML? The files are large and do require authentication. Here's my current code:

$username='username';
$password='password';
$location='http://www.example.com/file.xml.gz';


$ch = curl_init ();
curl_setopt($ch,CURLOPT_URL,$location);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER, 0);

$xmlcontent = curl_exec ($ch); 
curl_close($ch);

print_r($xmlcontent);

感谢您的帮助!

推荐答案

不知道为什么,但没有其他的答案对我有用。 zlib安装在服务器上,但gzdecode()函数没有在库中定义,gzuncompress给了我错误,compress.zlib://。

Not sure why, but none of the other answers worked for me in the end. zlib was installed on the server, but the gzdecode() function was not defined in the library, and the gzuncompress gave me errors, as did compress.zlib://. They might work for you so, give them a try as well.

如果你需要检查zlib是否安装了这个 stackoverflow answer 这个答案可以帮助。他们提供此脚本:

If you need to check if zlib is installed this stackoverflow answer or this answer can help. They provide this script:

<?php

echo phpversion().", ";

if (function_exists("gzdecode")) {
  echo "gzdecode OK, ";
} else {
  echo "gzdecode no OK, ";
}

if (extension_loaded('zlib')) {
  echo "zlib extension loaded ";
} else {
  echo "zlib extension not loaded ";
}

?>

这个网站提供了另一个脚本,显示zlib函数的安装:

This site gives another script that shows what zlib function are installed:

var_dump(get_extension_funcs('zlib'));

解决方案!这两个功能为我做了伎俩。只需curl或使用file_get_contents抓取xml文件,然后使用此脚本:

SOLUTION!!! These 2 functions did the trick for me. Just curl or use file_get_contents to grab the xml file, then use this script:

$xmlcontent = gzinflate(substr($xmlcontent,10,-8));

或者使用此脚本获取xml文件并获取内容(更多在这里):

OR use this script to grab the xml file and get the contents (see more here):

$zd = gzopen($filename,"r");
$contents = gzread($zd,$fileSize);
gzclose($zd);

感谢所有帮助我得到这个答案。希望这能帮助别人!

Thanks to all who helped me get this answer. Hope this helps someone else!

这篇关于使用curl PHP获取gzip压缩的XML文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆