从XML文件中获取一些数据并输入到sql中

时间:2015-03-23 00:28:54

标签: php mysql xml

我尝试将以下XML网址和一些数据加载到我的数据库中。

https://zkillboard.com/api/kills/corporationID/98115872/pastSeconds/7200/xml/

我有以下代码:

<?php 
// INCLUDE DB CONNECTION FILE
include("includes/connect.php");
// CHANGE THE VALUES HERE
include("includes/config.php");
$url = "https://zkillboard.com/api/kills/corporationID/98115872/pastSeconds/7200/xml/";
// RUN XML DATA READY FOR INSERT
$xml = simplexml_load_file($url);
// Loop Through Names
$insertValues = array();
$modifiedTS = date('Y-m-d h:i:s');
foreach ($xml->result->rowset[0] as $value)
{
        //Prepare the values
        $killID = $value['killID'];
        $solarSystemID = mysql_real_escape_string($value['solarSystemID']);
        $killTime = $value['killTime'];
        $moonID = $value['moonID'];
        $allianceName = $value['allianceName'];
        $corporationName = $value['corporationName'];
        $damageTaken = $value['damageTaken'];
//Create and run ONE INSERT statement (with UPDATE clause)
    $insert = "INSERT INTO `killLog` (killID,solarSystemID,killTime,moonID,allianceName,corporationName,damageTaken,last_modified) VALUES('$killID','$solarSystemID','$killTime','$moonID','$allianceName','$corporationName','$damageTaken','$modifiedTS') ON DUPLICATE KEY UPDATE 
last_modified = '$modifiedTS'";
mysql_query($insert) or die(mysql_error());
};
?>

但访问PHP文件时出现以下错误。

  

警告:   使用simplexml_load_file(https://zkillboard.com/api/kills/corporationID/98115872/pastSeconds/7200/xml/)   [function.simplexml-load-file]:无法打开流:HTTP请求   失败! HTTP / 1.1 406不可接受的编码。请使用gzip或   放气   /homepages/*****/*****/htdocs/*****/tool/admin/api_killLog.php   第14行

     

警告:simplexml_load_file()[function.simplexml-load-file]:I / O   警告:无法加载外部实体   “https://zkillboard.com/api/kills/corporationID/98115872/pastSeconds/7200/xml/”   在   /homepages/*****/*****/htdocs/*****/tool/admin/api_killLog.php   第14行

     

警告:为foreach()提供的参数无效   第19行//*/*****/htdoc/*****/tool/admin/api_killLog.php

我做错了什么?

1 个答案:

答案 0 :(得分:0)

这是在curl标题中设置deflate的一个简单例子。

我的代码是

function get_url_content( $url ) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'xxxxxx Scraper Tool 1.1');
    curl_setopt($ch, CURLOPT_ENCODING, 'deflate');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);

    $output = curl_exec($ch);

    if (curl_errno($ch)) {
        die('Curl error: ' . curl_error($ch));
    }

    if (function_exists('curl_getopt')) {
        $http_code = curl_getopt($ch, CURLINFO_HTTP_CODE);
        if ($http_code != 200) {
            die('Error response from their server - error code: ' . $http_code);
        }
    }

    curl_close($ch);

    return $output;
}

如果您需要携带编码,请在游戏中向我发送电子邮件,DaveTheGreat