How to extract data from a simple API Response?
I received an api response from the endpoint:
http://rest.sharethis.com/reach/getUrlInfo.php?url='http://www.mydomain.com/'
{ "email" : { "inbound" : 47498,
"outbound" : "27459"
},
"facebook" : { "inbound" : 6311,
"outbound" : "2301"
},
"other" : { "inbound" : 2196,
"开发者_开发技巧outbound" : "1456"
},
"sharethis" : { "inbound" : 16536,
"outbound" : "7746"
},
"total" : { "inbound" : 80399,
"outbound" : "40956"
},
"twitter" : { "inbound" : 4946,
"outbound" : "1298"
}
}
How can i get the sum of 'inbound + outbound' of the "total" variable into a string like $total for echoing out in php.
$decoded = json_decode($string);
$inbound = $decoded->total->inbound;
$outbound = $decoded->total->outbound;
echo "Total: ".($inbound+$outbound);
That appears to be JSON. You should check out json_decode(), which will turn your string into a PHP array that can be easily manipulated in the standard ways.
精彩评论