XML 解析 PHP 未捕获一个变量
XML parsing in PHP not capturing one variable
我在 PHP 中进行用户基本身份验证,并通过 XML 文件获取身份验证结果,然后将其解析为 PHP 变量。
这里是 PHP 代码:
<?php
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$str = $username . ":" . $password;
$client_id = md5($str);
$auth_str = base64_encode($str);
$client_id_str = "client-identifier: " . $client_id;
$plex_auth = "authorization: Basic " . $auth_str;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://someurl/sign_in.xml",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
$plex_auth,
$client_id_str,
"header3",
"header4"
),
));
$response = curl_exec($curl);
$xml = simplexml_load_string($response);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$user_token = $xml->authenticationtoken;
$user_email = $xml->email;
echo $user_email;
echo $user_token;
?>
这是 XML 中的响应:
<?xml version="1.0" encoding="UTF-8"?>
<user email="xyz@gmail.com" id="2567053"
thumb="https://secure.gravatar.com/avatar/somenumber"
username="Admin" title="Admin" cloudSyncDevice=""
authenticationToken="SomeToken" scrobbleTypes="">
<subscription active="1" status="Active" plan="lifetime">
<feature id="pass"/>
<feature id="sync"/>
<feature id="cloudsync"/>
<feature id="home"/>
</subscription>
<profile_settings auto_select_audio="1" auto_select_subtitle="0"
default_audio_language="en" default_subtitle_language="en"/>
<username>Admin</username>
<email>xyx@gmail.com</email>
<joined-at type="datetime">2014-05-08 17:59:24 UTC</joined-at>
<authentication-token>SomeToken</authentication-token>
</user>
现在由于某种原因,虽然我可以从 XML 中提取所有其他变量,但 authenticationToken
拒绝解析。当我回显 user_token
时,它完全是空的!
根据 SimpleXML 上的 PHP 文档示例:
Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
所以你可以这样访问它:
$user_token = $xml->{'authentication-token'};
您提供的 XML 中的响应似乎不是有效的 xml 字符串。
在 'user' 标记的末尾添加一个“>”之前,我无法解析它。
另外,正如我之前的回答中提到的,您将需要访问包含带有大括号和单引号的特殊字符的对象属性:
$user_token = $xml->{'authentication-token'};
或者像这样:
$auth_token = 'authentication-token';
$user_token = $xml->$auth_token;
我在 PHP 中进行用户基本身份验证,并通过 XML 文件获取身份验证结果,然后将其解析为 PHP 变量。
这里是 PHP 代码:
<?php
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
$str = $username . ":" . $password;
$client_id = md5($str);
$auth_str = base64_encode($str);
$client_id_str = "client-identifier: " . $client_id;
$plex_auth = "authorization: Basic " . $auth_str;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://someurl/sign_in.xml",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
$plex_auth,
$client_id_str,
"header3",
"header4"
),
));
$response = curl_exec($curl);
$xml = simplexml_load_string($response);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
$user_token = $xml->authenticationtoken;
$user_email = $xml->email;
echo $user_email;
echo $user_token;
?>
这是 XML 中的响应:
<?xml version="1.0" encoding="UTF-8"?>
<user email="xyz@gmail.com" id="2567053"
thumb="https://secure.gravatar.com/avatar/somenumber"
username="Admin" title="Admin" cloudSyncDevice=""
authenticationToken="SomeToken" scrobbleTypes="">
<subscription active="1" status="Active" plan="lifetime">
<feature id="pass"/>
<feature id="sync"/>
<feature id="cloudsync"/>
<feature id="home"/>
</subscription>
<profile_settings auto_select_audio="1" auto_select_subtitle="0"
default_audio_language="en" default_subtitle_language="en"/>
<username>Admin</username>
<email>xyx@gmail.com</email>
<joined-at type="datetime">2014-05-08 17:59:24 UTC</joined-at>
<authentication-token>SomeToken</authentication-token>
</user>
现在由于某种原因,虽然我可以从 XML 中提取所有其他变量,但 authenticationToken
拒绝解析。当我回显 user_token
时,它完全是空的!
根据 SimpleXML 上的 PHP 文档示例:
Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
所以你可以这样访问它:
$user_token = $xml->{'authentication-token'};
您提供的 XML 中的响应似乎不是有效的 xml 字符串。 在 'user' 标记的末尾添加一个“>”之前,我无法解析它。 另外,正如我之前的回答中提到的,您将需要访问包含带有大括号和单引号的特殊字符的对象属性:
$user_token = $xml->{'authentication-token'};
或者像这样:
$auth_token = 'authentication-token';
$user_token = $xml->$auth_token;