如何用PHP cURL和XML解析数据



我试图在https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users?limit=10&offset=0&order_by=last_name%2C%20first_name%2C%20primary_id&apikey=l8xx7b65eb296f6c43d1af7d005aac0dbcf8输出测试xml文件的所有last_name数据(它是供应商的客户url,因此包括apikey)xml文件为:

<users total_record_count="1304">
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/AASO1398626404">
<primary_id>AASO1398626404</primary_id>
<first_name>SORAYA</first_name>
<last_name>AABAIDA</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/ABAN1176297265">
<primary_id>ABAN1176297265</primary_id>
<first_name>ANNA;LAURE</first_name>
<last_name>ABADIE</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/ABOD1478476334">
<primary_id>ABOD1478476334</primary_id>
<first_name>ODILE;MARIE</first_name>
<last_name>ABADIE</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/2013-92">
<primary_id>2013-92</primary_id>
<first_name>ISABELLE FLORENCE</first_name>
<last_name>ABAR</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/2013-93">
<primary_id>2013-93</primary_id>
<first_name>ISABELLE FLORENCE</first_name>
<last_name>ABAR</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/idLDAP">
<primary_id>idLDAP</primary_id>
<first_name>ISABELLE FLORENCE</first_name>
<last_name>ABAR</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/ldap_80c6f387-dd82-4d5c-9ee6-eb3090f7a0f7">
<primary_id>ldap_80c6f387-dd82-4d5c-9ee6-eb3090f7a0f7</primary_id>
<first_name>KARINAR;</first_name>
<last_name>ABDEREMANAR</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/ldap_06107ebc-e3aa-44ba-aed7-1781c2eb6eb8">
<primary_id>ldap_06107ebc-e3aa-44ba-aed7-1781c2eb6eb8</primary_id>
<first_name>MYRIAM;</first_name>
<last_name>ABDERRAHIM</last_name>
<gender desc="Female">FEMALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/ldap_1b081623-e32d-46b6-a555-c86bb2bdb1a0">
<primary_id>ldap_1b081623-e32d-46b6-a555-c86bb2bdb1a0</primary_id>
<first_name>EL-AMINE;</first_name>
<last_name>ABDERREZAGUE</last_name>
<gender desc="Male">MALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
<user link="https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users/testLDAP">
<primary_id>testLDAP</primary_id>
<first_name>TAMYME;</first_name>
<last_name>ABDESSAMED</last_name>
<gender desc="Male">MALE</gender>
<password/>
<status desc="Active">ACTIVE</status>
<is_researcher>false</is_researcher>
</user>
</users>

,我试图输出它:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$ch = curl_init();
$baseUrl = 'https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users';
$templateParamNames = array('{user_id}');
$templateParamValues = array(urlencode('exl_impl'));
$baseUrl = str_replace($templateParamNames, $templateParamValues, $baseUrl);
$queryParams = array(
'user_id_type' => 'all_unique',
'view' => 'full',
'expand' => 'none',
'apikey' => 'l8xx7b65eb296f6c43d1af7d005aac0dbcf8'
);
$url = $baseUrl . "?" . http_build_query($queryParams);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

$oXML = new SimpleXMLElement( $response );
$i = 0;
foreach ($oXML->users as $user) 
{

echo last_name;
$i++;
} 
?>

…但我不能让它起作用。我希望有人能看到这个问题并提供帮助?由于

试试更简单的:

$lns = $oXML->xpath('//user/last_name');
foreach ($lns as $ln){
echo $ln . "n";

示例xml的输出:

AABAIDA
ABADIE
ABADIE

等。

最新更新