带有curl的simplexml,用api获取格式化的xml回调

时间:2013-10-29 03:28:38

标签: php xml curl

好的,所以我从我的卖家小组收到api电话进行托管。我想要做的是为我的国家实现这一目标:

<select name="countries">
  <option value="n226">United States</option>
</select>

其中value等于标记名称,并且显示等于每个国家/地区的名称。

我目前正在使用Curl和simplexml_load_string php代码。

function test() {

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "ApiCallUrl");
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, POST);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $string = curl_exec($ch);   

  $xml = simplexml_load_string($string);

  print_r($xml);
}

我上面的内容会返回它应该返回的内容(稍微混乱)

SimpleXMLElement Object
( [command] => 0 [error_code] => 0 [ttl] => 56000 [countries] => SimpleXMLElement Object ( [n0] => SimpleXMLElement Object ( [name] => Afghanistan [iso2] => AF [phone_code] => 93 )
    [n1] =&gt; SimpleXMLElement Object
        (
            [name] =&gt; Albania
            [iso2] =&gt; AL
            [phone_code] =&gt; 355
        )

简而言之,就是它目前的回报。

现在,当我按照php网站上的示例1中的建议将print_r($xml);更改为print_r($xml->name);时:http://php.net/manual/en/function.simplexml-load-string.php它只返回:SimpleXMLElement Object ( ),没有其他内容。

现在我只是想说出每个国家的名字,我现在不一定会担心选择。

那么为什么它适用于:print_r($xml);而不是print_r($xml->name);

顺便说一句:

在我对api做任何事情之前,它显示以下内容:(我通过在定义$ string后直接添加print_r($string);来实现此目的

<ampa>
<command></command>0
<error_code>0</error_code>
<ttl>56000</ttl>
<countries>
    <n0>
        <name>Afghanistan</name>
        <iso2>AF</iso2>
        <phone_code>93</phone_code>
    </n0>
    <n1>
        <name>Albania</name>
        <iso2>AL</iso2>
        <phone_code>355</phone_code>
    </n1>

0 个答案:

没有答案
相关问题