Problems in Consuming web service from PHP

0

Well I am new to this webservices and I have a webservice which gives me the following information

and I use the following PHP code to connect and make the PUT request in the webservice.

<?php



$pantalla= "zonas";
$id= "8";

// creo la url + el codigo que modificare

$url= "http://miwebservice.net/api/".$pantalla. "/" . $id ;

//construir el json que debo enviar en el body

    $ConstructorJson = array(
      'ZonaId' => $Datos['txt_codigo'],
      'Nombre' => $Datos['txt_Nombre'],
      'CreadoPor' => $Datos['txt_CreadoPor'],
      'CreadoFecha' => $Datos['txt_CreadoFecha'],
      'ModificadoPor' => $Datos['txt_ModificadoPor'],
      'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
      'Estado' => $Datos['cbo_Estado'],
      'Nota' => $Datos['txt_Notas']
    );
    
$json = json_encode($ConstructorJson);

    $curl = curl_init();
      curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "PUT",
          CURLOPT_POSTFIELDS => $json,
          CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "Accept: application/json\r\n",
          ),
      ));

      $response = curl_exec($curl);
      $err = curl_error($curl);

      curl_close($curl);

      if ($err) {
         echo "cURL Error #:" . $err;
      } else {
         echo $response;
      }    


?>

I get the following error

Length Required HTTP Error 411. The request must be chunked or have a content length.

Can someone help me solve it and tell me if the same code would work for the POST method?

    
asked by Wilfredo Aleman 17.06.2017 в 16:45
source

2 answers

0

Good Finally I solved the problem

I attach the code

<?php


function jsonZonaPUT($Datos)
{
//url del webservice + tabla + id del registro a modificar
  $url = "mywebservice.com/zonas/8";

//contruir el arreglo de los datos que enviare 

  $ConstructorJson = array(
      'Nombre' => $Datos['txt_Nombre'],
      'CreadoPor' => $Datos['txt_CreadoPor'],
      'CreadoFecha' => $Datos['txt_CreadoFecha'],
      'ModificadoPor' => $Datos['txt_ModificadoPor'],
      'ModificadoFecha' => $Datos['txt_ModificadoFecha'],
      'Estado' => $Datos['cbo_Estado'],
      'Nota' => $Datos['txt_Notas']
    );
    
// convertir el array a json
  $json = json_encode($ConstructorJson);
  
//crear el arreglo para enviar los datos via Curl
    $curl = curl_init();
      curl_setopt_array($curl, array(
          CURLOPT_URL => $url,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "PUT",
          CURLOPT_POSTFIELDS => $json,
          CURLOPT_HTTPHEADER => array(
            'Accept: application/json',
            'Content-Type: application/json',

          ),
      ));
      
      // si existe algun problema lo imprimira a pantalla sino imprimira el json posteado en webservice
      $response = curl_exec($curl);
      $err = curl_error($curl);

      curl_close($curl);

      if ($err) {
         echo "cURL Error #:" . $err;
      } else {
         echo $response;
      }


}

?>
    
answered by 19.06.2017 / 23:00
source
0

I think you have to correct a couple of things:

1) So it says the parameter ZonaId is passed in the query of the URL only, in the code is also in the body.

2) If you are going to send json, you should have a header of Content-Type: application/json

3) Try the two previous changes. If you follow the error of content length , add a header with Content-Length: 999 . 999 Is the number of bytes you send. You have to take the body already encoded as json and place the length of the string with mb_strlen() .

4) Check if you have problems with the dates in the json. This format can serve as a reference: 2016-08-01T14:25:43.000Z

    
answered by 17.06.2017 в 17:36