我在使用Linkedin API -> https://developer.linkedin.com/documents/profile-fields#company
一切都很好,我可以毫无问题地连接到API。当我请求一些数据时,Bellow遵循API返回的数据。
这是我的请求(我不会发布整个代码,它超过500行),但这是我想要检索的内容的本质。
$response2 = $OBJ_linkedin->profile('~:(recommendations-received)');
if($response2['success'] === TRUE)
{
$response2['linkedin'] = new SimpleXMLElement($response2['linkedin']);
echo "<pre>" . print_r($response2['linkedin'], TRUE) . "</pre>";
}
else
{
// request failed
echo "Error: <br />RESPONSE:<br /><br /><pre>" . print_r($response2) . "</pre>";
}上面的遵循响应:
SimpleXMLElement Object
(
[recommendations-received] => SimpleXMLElement Object
(
[@attributes] => Array
(
[total] => 1
)
[recommendation] => SimpleXMLElement Object
(
[id] => 0123456
[recommendation-type] => SimpleXMLElement Object
(
[code] => service-provider
)
[recommendation-text] => Here come the recommendation text from the API.
[recommender] => SimpleXMLElement Object
(
[id] => npAnFGrTys
[first-name] => John
[last-name] => Doe
)
)
)
)现在我的问题是:
如何仅检索和打印推荐文本节点?我已经只打电话给推荐-收到的$response2 = $OBJ_linkedin->profile('~:(recommendations-received)');,但它返回整个事情,然后如何只得到[recommendation-text]?
我尝试过simplexmlelement (http://br2.php.net/manual/en/class.simplexmlelement.php),但没有成功。
谢谢您的任何帮助。
结果
我会把得到的答案寄给我。
我使用的代码如下建议的@Mircea:
$sxml = new SimpleXMLElement($response2['linkedin']);
$res = $sxml->xpath('recommendations-received/recommendation/recommendation-text');
echo $res[0];而不是这段代码:
$response2['linkedin'] = new SimpleXMLElement($response2['linkedin']);
echo "<pre>" . print_r($response2['linkedin'], TRUE) . "</pre>";这里的不同之处在于,我现在使用xpath方法搜索simpleXML节点以寻找子节点匹配。谢谢@Mircea。
发布于 2012-04-16 15:42:35
您应该尝试使用simplexmlelement http://www.php.net/manual/en/simplexmlelement.xpath.php的xpath函数。我认为得到你想要的东西的方法是:
$sxml->xpath('recommendations-received/recommendation/recommendation-text')它返回一个数组,因此您应该对它进行迭代(查看该页面上的示例)。xpath查询就是这样的,它最终取决于所接收的xml的结构。
希望能帮上忙。
发布于 2012-04-16 16:53:36
访问recommendation-text属性的方法如下:
foreach($response2->{recommendations-received} as $recommendation) {
$recommendation->{recommendation-text}
}https://stackoverflow.com/questions/10176981
复制相似问题