#php #xml #curl
Вопрос:
Я пытаюсь вывести все last_name
данные для тестового xml-файла по адресу https://api-eu.hosted.exlibrisgroup.com/almaws/v1/users?limit=10amp;offset=0amp;order_by=last_name, first_name, primary_idamp;apikey=l8xx7b65eb296f6c43d1af7d005aac0dbcf8 (это гостевой 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 ;
}
?>
…но не могу заставить его работать. Я надеялся, что кто-нибудь сможет увидеть проблему и помочь?
Спасибо
Ответ №1:
Попробуйте что-нибудь попроще:
$lns = $oXML->xpath('//user/last_name');
foreach ($lns as $ln){
echo $ln . "n";
Вывод из вашего образца xml:
AABAIDA
ABADIE
ABADIE
и т.д.
Комментарии:
1. Спасибо, Джек. Отлично работает!