0

使用带有 api 令牌的 curl 并将 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);成我可以使用的变量?最终,我想显示、编辑或存储这些人,因为我将与高层同步网站数据库 - 这样便笺和联系人不会过时。

4

1 回答 1

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 />";
}
于 2013-02-23T21:23:52.657 回答