Ver Mensaje Individual
  #4 (permalink)  
Antiguo 28/07/2015, 13:25
Avatar de chronos682
chronos682
 
Fecha de Ingreso: febrero-2004
Ubicación: Tunja - Boyacá - Colombia
Mensajes: 627
Antigüedad: 20 años, 2 meses
Puntos: 69
Respuesta: Error unexpected T_STRING, expecting T_FUNCTION

Aquí faltan las comillas dobles de cierre:

Código PHP:
Ver original
  1. function sendResponse($status = 200, $body = '', $content_type = 'text/html'")

Puede ser ahí el error.

Aunque no me queda claro si son 2 parámetros ($status y $body) o son 3 ($status, $body y $content_type) porque si son 3 debería ser así:

function sendResponse($status = 200, $body = "", $content_type = "text/html")

Y la llamada a la función estaría mal porque solo estas psando 2 parámetros y no 3.

Corrijo: no hacen falta comillas de cierre, es que parecía que fuera una comilla doble pero en realidad son dos simples seguidas. Sigo mirando a ver dónde puede ser el error.


Tienes un error en las llaves de cierre. No sé si estas dos líneas van dentro de la clase o fuera de ella:

Código PHP:
Ver original
  1. sendResponse(400, "Invalid request");
  2. return false;

Si van fuera de la clase debería quedar así el código:

Código PHP:
Ver original
  1. <?php
  2.  
  3.  
  4. function getStatusCodeMessage($status)
  5. {
  6.  
  7. $codes = Array(
  8. 100 => "Continue",
  9. 101 => "Switching Protocols",
  10. 200 => "OK",
  11. 201 => "Created",
  12. 202 => "Accepted",
  13. 203 => "Non-Authoritative Information",
  14. 204 => "No Content",
  15. 205 => "Reset Content",
  16. 206 => "Partial Content",
  17. 300 => "Multiple Choices",
  18. 301 => "Moved Permanently",
  19. 302 => "Found",
  20. 303 => "See Other",
  21. 304 => "Not Modified",
  22. 305 => "Use Proxy",
  23. 306 => "(Unused)",
  24. 307 => "Temporary Redirect",
  25. 400 => "Bad Request",
  26. 401 => "Unauthorized",
  27. 402 => "Payment Required",
  28. 403 => "Forbidden",
  29. 404 => "Not Found",
  30. 405 => "Method Not Allowed",
  31. 406 => "Not Acceptable",
  32. 407 => "Proxy Authentication Required",
  33. 408 => "Request Timeout",
  34. 409 => "Conflict",
  35. 410 => "Gone",
  36. 411 => "Length Required",
  37. 412 => "Precondition Failed",
  38. 413 => "Request Entity Too Large",
  39. 414 => "Request-URI Too Long",
  40. 415 => "Unsupported Media Type",
  41. 416 => "Requested Range Not Satisfiable",
  42. 417 => "Expectation Failed",
  43. 500 => "Internal Server Error",
  44. 501 => "Not Implemented",
  45. 502 => "Bad Gateway",
  46. 503 => "Service Unavailable",
  47. 504 => "Gateway Timeout",
  48. 505 => "HTTP Version Not Supported"
  49. );
  50.  
  51. return (isset($codes[$status])) ? $codes[$status] : '';
  52. }
  53.  
  54. // Helper method to send a HTTP response code/message
  55. function sendResponse($status = 200, $body = "", $content_type = "text/html")
  56. {
  57. $status_header = "HTTP/1.1 " . $status . " " . getStatusCodeMessage($status);
  58. header($status_header);
  59. header("Content-type: " . $content_type);
  60. echo $body;
  61. }
  62.  
  63. class RedeemAPI {
  64.     private $db;
  65.     // Constructor - open DB connection
  66.     function __construct()
  67.     {
  68.         $this->db = new mysqli("localhost", "futchoco_admin", "Futcho190867", "futchoco_futsoft");
  69.         /* verificar la conexión */
  70.         if (mysqli_connect_errno())
  71.         {
  72.             printf("Conexión fallida: %s\n", mysqli_connect_error());
  73.             exit();
  74.         }
  75.         $this->db->autocommit(FALSE);
  76.     }
  77.  
  78.     // Destructor - close DB connection
  79.     function __destruct()
  80.     {
  81.         $this->db->close();
  82.     }
  83.     // Main method to redeem a code
  84.     function redeem()
  85.     {
  86.         // Check for required parameters
  87.         $json = file_get_contents("php://input");
  88.         $obj = json_decode($json,true);
  89.         echo $obj;
  90.         $rows = array();
  91.         //foreach($array[] as $key => $value)
  92.         foreach($obj as $item)
  93.         {
  94.             $rows[] = "('" . $key . "', '" . $value . "')";
  95.             $stmt = $this->db->prepare('INSERT INTO prueba (id,nombre)
  96.             VALUES (%d,%s)',$item->id_cliente,$item->nombre) or die(mysqli_error($this->db));
  97.             $stmt->execute();
  98.         }
  99.     }
  100. }
  101.     sendResponse(400, "Invalid request");
  102.     return false;
  103.  
  104.  
  105. // This is the first thing that gets called when this page is loaded
  106. // Creates a new instance of the RedeemAPI class and calls the redeem method
  107. $api = new RedeemAPI;
  108. $api->redeem();
  109.  
  110. ?>

Acostúmbrate a tabular las líneas para que no te pase eso.
__________________
Si te gustó la respuesta dale +1

HERNÁN G. SIABATO M.
[email protected]

Última edición por chronos682; 28/07/2015 a las 13:52