Ask your WordPress questions! Pay money and get answers fast! (more info)

Extracting a value from an XML result

  • SOLVED

Given an XML result such as that seen at [[LINK href="http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techcrunch.com"]]http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techcrunch.com[[/LINK]] how is it best to extract and echo the 'like_count' value? Thanks.

Answers (2)

2015-09-03

Gabriel Reguly answers:

Use [[LINK href="https://developer.wordpress.org/reference/functions/wp_remote_get/"]]https://developer.wordpress.org/reference/functions/wp_remote_get/[[/LINK]]


designbuildtest comments:

Thanks Gabriel, not really a complete answer, but it did lead me to [[LINK href="http://stackoverflow.com/questions/15312554/using-wp-remote-get-to-work-with-xml-in-wordpress"]]this entry on StackOverflow[[/LINK]] which in turn enabled me to produce:


$url = 'http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techcrunch.com';
$response = wp_remote_get( $url );
$body = wp_remote_retrieve_body( $response );
$xml = simplexml_load_string( $body );
$like_count = $xml->link_stat->like_count;

echo $like_count;


designbuildtest comments:

Marking this one as solved. Thanks Gabriel.

2015-09-03

Arnav Joy answers:

what is the output of this

<?php

$xml = simplexml_load_string("http://api.facebook.com/restserver.php?method=links.getStats&urls=http://www.techcrunch.com");
if ( $xml ) {
print_r($xml);

}


designbuildtest comments:

Produces an error message unfortunately ... simplexml_load_string(): Entity: line 1: parser error