Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过CURL从API返回的xml的表格式_Php_Xml_Curl_Html Table - Fatal编程技术网

Php 通过CURL从API返回的xml的表格式

Php 通过CURL从API返回的xml的表格式,php,xml,curl,html-table,Php,Xml,Curl,Html Table,我正在使用另一家公司的API从他们的数据库中检索信息,它将以XML格式返回给我。我正试图完成两件事——但我有几个问题 1st我想将原始XML数据格式化为表格格式,以便通过浏览器查看 2nd我收到的数据是ID/用户名/密码/电子邮件。我希望能够将这些数据导入到我的数据库中,这样每个用户ID都是插入到数据库中的一行(我可以做数据库工作,我只是不知道如何单独处理每个用户) API格式是这样的,只有数百个用户的API而不是一个 每当我打印$array时,我都会按预期将数据作为一个大的blob。但是,当我

我正在使用另一家公司的API从他们的数据库中检索信息,它将以XML格式返回给我。我正试图完成两件事——但我有几个问题

1st我想将原始XML数据格式化为表格格式,以便通过浏览器查看

2nd我收到的数据是ID/用户名/密码/电子邮件。我希望能够将这些数据导入到我的数据库中,这样每个用户ID都是插入到数据库中的一行(我可以做数据库工作,我只是不知道如何单独处理每个用户)

API格式是这样的,只有数百个用户的API而不是一个

每当我打印$array时,我都会按预期将数据作为一个大的blob。但是,当我使用更新后的代码时,我收到一个错误,即用户不是有效的索引。我还收到看起来是表的开始的内容,其中没有任何数据(只有边框)

如果有人能帮我找出表格没有接收数据的原因(或者给我一个更好的方法),我将不胜感激

任何能帮我解决第二个问题的人都可以得到额外的分数

错误是
注意:未定义的索引:第36行/home/public\u html/new/test.php中的用户
第36行在代码中有注释

下面是我的代码的底部部分:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Method execution
$result = curl_exec($ch);
// Close CURL session

$array = json_decode(json_encode((array)simplexml_load_string($result)),1);

$array_user=$array['user']; //line 36

$tab='<table border="1" width="400">';
for ($j=1; $j< count($array_user) ; $j++) {

  $tab.='<tr>';
  $tab.='<td>'.$array_user[$j]['id'].'</td>';
  $tab.='<td>'.$array_user[$j]['login'].'</td>';
  $tab.='<td>'.$array_user[$j]['mail'].'</td>';
 $tab.='<td>'.$array_user[$j]['date'].'</td>';
 $tab.='</tr>';
}

$tab.='</table>';

echo $tab;


?>
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
//方法执行
$result=curl\u exec($ch);
//闭卷会话
$array=json_decode(json_encode((array)simplexml_load_string($result)),1);
$array_user=$array['user']//第36行
$tab='';
对于($j=1;$j

这就是如何使用
simplexml

您可以从中构建表或查询

$xml = simplexml_load_string($x); // XML is in $x

foreach ($xml->user as $user) {

    echo $user->id . ': ' . $user->login . ' : '
    echo $user->password . ' : ' . $user->message . '<br />;
}
$xml=simplexml\u load\u string($x);//XML的单位是$x
foreach($xml->user as$user){
echo$user->id.:'.$user->login.:'
回显$user->password.:'.$user->message.'
; }
看到它工作了吗

顺便说一句:您的xml需要修复:

<API>
    <user>
        <id>1</id>
        <login>michi</login>
        <password>12345</password>
        <message>hi!</message>
    </user>
</API>

1.
米奇
12345
你好

$sxe=simplexml\u load\u string('

您的xml已损坏,请参见第一个
,并且
节点未关闭,因此类似于这样?
$xml=simplexml\u load\u string($result);//xml在$x foreach($xml->user as$user)中{echo$user->id'.:'.$user->login'.:'.$user->password'.:'.$user->message'.
;}
注意这一点很重要-用于正确使用,它们意味着这是从API发送的消息,没有用户数据,它只是一个类似@RobertDickey:
foreach($xml->message->user as$user)的封闭符
我爱你。非常感谢。我还想说,这非常有效,我真的很感谢你的帮助。如果我能给你代表,我会的。
$sxe = simplexml_load_string('<API><message><user><id>10</id><login>a</login><password>abc</password></user><user><id>11</id><login>456</login><password>def</password></user></message></API>');

// or $sxe = simplexml_load_file($xml_url);

function tabulate(SimpleXMLElement $sxe) {
    if (!count($sxe)) return '';
    $table = "<table border='1' width='400'>\n";
    $header = false;
    foreach($sxe as $row) {
        if (!$header) {
            $table .= "<thead>\n<tr>\n\t";
            foreach ($row as $field) {
                $table .= "<th>";
                $table .= htmlspecialchars($field->getName(), ENT_NOQUOTES, 'UTF-8');
                $table .= "</th>";
            }
            $table .= "\n</tr>\n</thead>\n<tbody>\n";
            $header = true;
        }
        $table .= "<tr>\n\t";
        foreach ($row as $field) {
            $table .= "<td>";
            $table .= htmlspecialchars((string) $field, ENT_NOQUOTES, 'UTF-8');
            $table .= "</td>";
        }
        $table .= "\n</tr>\n";

    }
    $table .= "</tbody>\n</table>";
    return $table;
}


function insert_users(PDO $db, SimpleXMLElement $users) {
    $ins = $db->prepare('INSERT INTO users (id, login, password) VALUES (?,?,?)');
    foreach ($users as $user) {
        $userdata = array((string) $user->id, (string) $user->login, (string) $user->password);
        $ins->execute($userdata);
    }
}

insert_users($db, $sxe->message->user);

echo tabulate($sxe->message->user);