Gracias por contestar!
Nosotros tenemos un objeto HttpRequest.php que armamos nuestra consulta a la API:
<?php
namespace controllers;
/*
* simple HttpRequest example using PHP
* tom slankard
*/
class HttpRequest {
public $url = null;
public $method = 'GET';
public $body = null;
public $headers = Array();
public $allow_redirect = true;
private $url_info = null;
private $host_name = null;
private $host_ip = null;
public $response_body = null;
private $response_headers = Array();
private $response_code = null;
private $response_message = null;
private $port = null;
private $verbose = true;
private $flagLeerBody = false;
public function __construct($url, $method = 'GET', $user = null, $pass = null) {
$this->flagLeerBody = false;
$this->url = $url;
$this->method = $method;
// parse url
$this->url_info = parse_url($url);
$this->host_name = $this->url_info['host'];
$this->host_ip = gethostbyname($this->host_name);
// get port number given the scheme
if(!isset($this->url_info['port'])) {
if($this->url_info['scheme'] == "http")
$this->port = 80;
else if($this->url_info['scheme'] == "https")
$this->port = 443;
} else {
$this->port = $this->url_info['port'];
}
// add default headers
$this->headers["Host"] = "$this->host_name";
$this->headers["Accept-Charset"] = "ISO-8859-1, utf-8;q=0.66, *;q=0.66";
// $this->headers["Connection"] = "close";
// Set Authorization if user is not null
if(!is_null($user))
{
$encode = base64_encode($user.':'.$pass);
$this->headers["Authorization"] = "Basic $encode";
}
}
private function constructRequest() {
$path = "/";
if(isset($this->url_info['path']))
{
$path = $this->url_info['path'];
/* Se agrega nueva funcionalidad para pasar querys a la url */
if(isset($this->url_info['query']))
{
$path .= "?".$this->url_info['query'];
}
}
$req = "$this->method $path HTTP/1.1\r\n";
foreach($this->headers as $header => $value) {
$req .= "$header: $value\r\n";
}
return "$req\r\n";
}
/// reads a line from a file
function readLine($fp)
{
$line = "";
while (!feof($fp)) {
// $fgetsLine = fgets($fp, 2048);
$fgetsLine = fgets($fp, 128);
// $fgetsLine = fgets($fp);
/*
No comprendo porque en el fgets antes del body encuentra un número "random" y rompe el json
Si un número en la línea, lo elimina para no romper la estructura del json.
*/
if(!is_numeric(trim($fgetsLine)))
{
$line .= $fgetsLine;
}
if (substr($line, -1) == "\n") {
return trim($line, "\r\n");
}
}
return $line;
}
public function send() {
$fp = fsockopen($this->host_ip, $this->port, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
\n";
} else {
// construct request
$request = $this->constructRequest();
// write request to socket
// fwrite($fp, $request);
fputs($fp, $request);
if(!empty($this->body))
{
fputs($fp, $this->body);
}
// read the status line
$line = $this->readline($fp);
$status = explode(" ", $line);
// make sure the HTTP version is valid
if(!isset($status[0]) || !preg_match("/^HTTP\/\d+\.?\d*/", $status[0]))
die("Couldn't get HTTP version from response.");
// get the response code
if(!isset($status[1]))
die("Couldn't get HTTP response code from response.");
else $this->response_code = $status[1];
// get the reason, e.g. "not found"
if(!isset($status[2]))
die("Couldn't get HTTP response reason from response.");
else $this->response_reason = $status[2];
// read the headers
do {
$line = $this->readLine($fp);
if($line != "") {
$header = explode(":", $line);
$this->response_headers[$header[0]] = ltrim($header[1]);
}
} while(!feof($fp) && $line != "");
// read the body
$this->response_body = "\n";
do {
$line = $this->readLine($fp); {
/*
@TODO: Comprender y corregir por qué dentro del body de vuelve una cadena de 717d antes del comienzo del json
*/
// var_dump($line);
// var_dump($this->limpiarJSONToken($line));
if($this->empiezaJson($line))
{
$this->flagLeerBody = true;
}
if($this->flagLeerBody)
{
if($line)
{
$this->response_body .= "$line\n";
}
}
}
} while(!feof($fp));
// close the connection
fclose($fp);
return TRUE;
}
}
public function setBody($body) {
$this->body = $body;
}
public function getResponseStatus() {
return $this->response_code;
}
public function getHeaders() {
return $this->response_headers;
}
public function getResponseBody() {
return $this->response_body;
}
/* Funcion auxiliar, busca si es un [ */
private function empiezaJson($string)
{
return strpos($string, "[") !== false || strpos($string, "{") !== false;
}
}
?>
Luego creamos ese objeto pasandole la url a la API, el método (GET), usuario y contraseña.
$req = new HttpRequest($url, $method, $usuario_rest, $pass_rest);
$req->setBody($body);
// $req->headers["Connection"] = "close";
$req->headers["Cache-Control"] = "no-cache";
if(count($headers) > 0){
foreach ($headers as $key => $value) {
$req->headers[$key] = $value;
}
}
if(count($body) > 0)
{
$encodeBody = json_encode($body);
$req->setBody($encodeBody);
$req->headers["Content-Length"] = strlen($encodeBody);
}
$req->send() or die("Couldn't send!");
$response = $req;
return $response;
No, no hemos probado con otro cliente. En otras consultas (otras URLs) devuelve bien. Solamente sucede con esa consulta a la API (la de recibos).
Espero que se comprenda, sino escribo con más detalle.
Gracias por tu colaboración!!