Highrise API - 如何使用XML和操作变量

时间:2013-02-23 03:26:49

标签: php xml api parsing

将curl与api令牌一起使用,并将XML元素转换为工作变量或至少以html格式查看。

以下是使用vardump的代码:

$curl = curl_init('https://mywebsite.highrisehq.com/people.xml');
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_USERPWD,'myapitoken12345:x');
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,0);

$data = curl_exec($curl);
curl_close($curl);  

$people_xml = new SimpleXMLElement($data);
var_dump($people_xml);

执行var_dump时,输出不是我熟悉的。

object(SimpleXMLElement) #1 (2) { 
["@attributes"]=> array(1) { 
["type"]=> string(5) "array" } 
["person"]=> array(128) { ...  etc. etc. etc...

HighriseHQ.com显示的XML结构就是如何检索元素的线索。

<people type="array">
  <person>
  <first-name>John</first-name>
  </person>
  <person>
  <first-name>Jane</first-name>
  </person>
</people>
  .... etc. etc.

我如何或在哪里将$people = simplexml_load_string($xml);部分翻译成我可以使用的变量?最终我想显示,编辑或存储这些人,因为我将使用高层同步网站数据库 - 这样笔记和联系人都不会过时。

1 个答案:

答案 0 :(得分:0)

找到了解决方案。

$curl = curl_init('https://my_website_goes_here.highrisehq.com/people.xml');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, 'my_api_token_goes_here:x');

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$data = curl_exec($curl);
curl_close($curl);

$people_xml = new SimpleXMLElement($data);

foreach($people_xml->person as $person) {
    echo "<strong>" . $person->{'first-name'} . " " . $person->{'last-name'} . "</strong><br />";
    echo "Title: " . $person->{'title'} . "<br />";
    echo "Background: " . $person->{'background'} . "<br />";
    echo "Email: " . $person->{'email-address'} . "<br />";
    echo "ID: " . $person->id . "<br /><br />";
}