Tema: PHP y JSON
Ver Mensaje Individual
  #5 (permalink)  
Antiguo 01/06/2011, 23:39
marcofbb
Invitado
 
Mensajes: n/a
Puntos:
Respuesta: PHP y JSON

Te dejo un ejemplo :)
aver si encontras tu error :P
Código PHP:
Ver original
  1. <?php
  2. # Funcion para buscar en Google con Json
  3. function buscar($aQue){
  4. $aUrl = "http://ajax.googleapis.com/ajax/services/search/web";
  5.  
  6. $data = file_get_contents($aUrl."?v=1.0&q=".$aQue); // Obtenemos el resultado
  7. $obj = json_decode($data); // Decodificamos el Json
  8. $results = $obj->responseData->results; // Entramos al array de los resultados
  9. # Hacemos un bucle para obtener los 4 resultados que muestra
  10. for ($i=0; $i<sizeof($results); $i++) {
  11.     $tmp = $results[$i];
  12.     $tmpHTML.= "<a href=\"".$tmp->url."\">".$tmp->title."</a>";
  13.     $tmpHTML.= "<br />".$tmp->content."<br/>";
  14.     $tmpHTML.= "<i>".$tmp->url."</i><br /><br />";
  15. }
  16. echo utf8_decode($tmpHTML); // Imprimimos los 4 resultados y le correjimos los acentos con utf8
  17. }
  18. ?>
  19. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  20. <html xmlns="http://www.w3.org/1999/xhtml">
  21. <head>
  22. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  23. <title>JSON con PHP</title>
  24. <link href="styles.css" rel="stylesheet" type="text/css" />
  25. </head>
  26. <body>
  27. <div id="search">
  28. <form method="get" action="">
  29. Buscador JSON
  30. <input type="text" id="q" name="q" value="<?php if(isset($_GET['q'])){ echo $_GET['q']; }?>" />
  31. <input type="submit" id="bt" name="bt" value="Buscar" />
  32. </form>
  33. </div>
  34. <div id="results">
  35. <?php
  36. if($_GET['q'])
  37. {
  38. buscar($_GET['q']);
  39. }
  40. ?>
  41. </div>
  42. </body>
  43. </html>