0

测试.xml

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>

    <item>
        <title>Hello world!</title>
        <link>http://localhost/wordpress/?p=1</link>
        <category><![CDATA[Uncategorized]]></category>
        <HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>
    </item>
</rss>

我用那个代码读取xml文件

    <?php
      if (file_exists('test.xml')) {
        $xml = simplexml_load_file('test.xml');
          echo "<pre>";
          print_r($xml);
          echo "</pre>";
      } else {
         exit('Failed to open test.xml.');
         }
     ?>

输出

      SimpleXMLElement Object
 (
 [@attributes] => Array
    (
        [version] => 2.0
    )

[item] => SimpleXMLElement Object
    (
        [title] => Hello world!
        [link] => http://localhost/wordpress/?p=1
        [category] => SimpleXMLElement Object
            (
            )

        [HEADLINE] => SimpleXMLElement Object
            (
            )

    )

 )

但我对 xml 中的那些标签有疑问

        <category><![CDATA[Uncategorized]]></category>
    <HEADLINE><![CDATA[Does Sleep Apnea Offer Some Protection During Heart Attack?]]></HEADLINE>

 to read data bcoz of it <![CDATA[]] in category and HEADLINE

我需要输出

        SimpleXMLElement Object
   (
 [@attributes] => Array
    (
        [version] => 2.0
    )

[item] => SimpleXMLElement Object
    (
        [title] => Hello world!
        [link] => http://localhost/wordpress/?p=1
        [category] => Uncategorized
        [HEADLINE] => Does Sleep Apnea Offer Some Protection During Heart Attack?
    )

 )

<?php

$rpxApiKey = 'YOUR_API_KEY_HERE';

if(isset($_POST['token'])) {

  /* STEP 1: Extract token POST parameter */
  $token = $_POST['token'];

  /* STEP 2: Use the token to make the auth_info API call */
  $post_data = array('token' => $_POST['token'],
                     'apiKey' => $rpxApiKey,
                     'format' => 'json');

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
  curl_setopt($curl, CURLOPT_HEADER, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  $raw_json = curl_exec($curl);
  curl_close($curl);


  /* STEP 3: Parse the JSON auth_info response */
  $auth_info = json_decode($raw_json, true);

  if ($auth_info['stat'] == 'ok') {

    /* STEP 3 Continued: Extract the 'identifier' from the response */
    $profile = $auth_info['profile'];
    $identifier = $profile['identifier'];

    if (isset($profile['photo'])) {
      $photo_url = $profile['photo'];
    }

    if (isset($profile['displayName'])) {
      $name = $profile['displayName'];
    }

    if (isset($profile['email'])) {
      $email = $profile['email'];
    }

    print $identifier;
        echo "</br>";
    print $photo_url;
        echo "</br>";
    print $name;
        echo "</br>";
    print $email;
    /* STEP 4: Use the identifier as the unique key to sign the user into your system.
This will depend on your website implementation, and you should add your own
code here.
*/

/* an error occurred */
} else {
  // gracefully handle the error. Hook this into your native error handling system.
  echo 'An error occured: ' . $auth_info['err']['msg'];
}
}
?>

这将从我们在身份验证后获得的令牌中提取信息。

4

3 回答 3

3

加载文件时试试这个

$xml = simplexml_load_file($this->filename,'SimpleXMLElement', LIBXML_NOCDATA);
于 2012-10-25T07:07:14.857 回答
0

试试这个:

var_dump(
    (string)$xml->item->category,
    (string)$xml->item->HEADLINE
);

SimpleXML 动态构建东西是毫无价值的,因此print_r()对象不一定显示有用的信息。

于 2012-10-25T07:20:39.503 回答
-1

尝试铸造

[category] => SimpleXMLElement Object
            (
            )

        [HEADLINE] => SimpleXMLElement Object
            (
            )

到字符串,我想你会得到你需要的

$item['category']=(string)$item['category'];
于 2012-10-25T07:08:31.833 回答