Foro de elhacker.net

Programación => PHP => Mensaje iniciado por: .:Weeds:. en 7 Noviembre 2013, 22:48 pm



Título: Carga Asyncronica Json
Publicado por: .:Weeds:. en 7 Noviembre 2013, 22:48 pm
Tengo el siguiente codigo.
Código
  1. <?
  2. function get_tweets($url) {
  3.  
  4.    $json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
  5.    $json = json_decode($json_string, true);
  6.  
  7.    return intval( $json['count'] );
  8. }
  9.  
  10. function get_likes($url) {
  11.  
  12.    $json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
  13.    $json = json_decode($json_string, true);
  14.  
  15.    return intval( $json[$url]['shares'] );
  16. }
  17.  
  18. function get_plusones($url) {
  19.  
  20.    $curl = curl_init();
  21.    curl_setopt($curl, CURLOPT_URL, "https://clients6.google.com/rpc");
  22.    curl_setopt($curl, CURLOPT_POST, 1);
  23.    curl_setopt($curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
  24.    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  25.    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  26.    $curl_results = curl_exec ($curl);
  27.    curl_close ($curl);
  28.  
  29.    $json = json_decode($curl_results, true);
  30.  
  31.    return intval( $json[0]['result']['metadata']['globalCounts']['count'] );
  32. }
  33. function get_shares($url) {    
  34.  $json_string = file_get_contents("http://www.linkedin.com/countserv/count/share?url=$url&format=json");
  35.  $json = json_decode($json_string, true);
  36.  return intval( $json['count'] );
  37. }
  38. ?>
  39.  

El code funciona de maravilla pero hasta que no carga el json no empieza a cargar la web, lo que hace que se haga bastante lenta, como podria hacerlo asyncronicamente? Estoy empezando con php.

Saludos y gracias.