开发者

Can't parse SimpleXML from Basecamp API in PHP

I'm using CodeIgniter and a Basecamp class written for it to connect to and retrieve data from the Basecamp API. I am connecting fine and grabbing data fine and its being returned using SimpleXML (you can specify XML or SimpleXML in the request).

I am just having a big problem getting anything good out of the response. Here is what the response looks like raw: http://pastie.org/private/bbxhgbzbbbk77ji3ua4g and view source: http://pastie.org开发者_开发问答/private/qftl28osnumhrdwr1zxuw

Obviously I see those via a print_r command.

Can someone tell me, for instance, how to get a list of the project names out of that?

I can also make the request in XML if that works better.


We can load the 'body' string from the response into a new SimpleXML object and iterate over the 'project' nodes to get project information. Here it seems that you have already loaded the SimpleXML object, so we just have to do the rest.

$projects = array();
foreach($response['body']->project as $_xml) {
    //parse the project xml into array
    $projects[] = xml2array($_xml);
}
print_r($projects);

//list the project titles
foreach($projects as $project) {
    echo $project['name'] .'<br/>';
}

//function to parse a xml object to array: http://php.net/manual/en/ref.simplexml.php
function xml2array ($xmlObject, $out = array())
{
    foreach ((array)$xmlObject as $index => $node) {
        $out[$index] = (is_object($node)) ? xml2array ($node) : $node;
    }
    return $out;
}

cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜