CURL PHP request for a WEB Service

1

Good morning, everyone. Guys, I'm having a problem communicating with webservice from a third-party company here where I work. Explaining above, I need to send a transaction report with coupons that have been identified with the cashback option and send via WS to this company to account for and generate the credit in the customers' account. The generation of the file and everything else is ready, the problem is in sending the file to WS.

Here is my code that sends the file:

        // URL COMPLETA: https://integration-staging.meliuz.com.br/base2/superrocha/
        $url = 'https://integration-staging.meliuz.com.br/base2/superrocha/';

        // O Token que é privado e não posso compartilhar aqui
        $header = array(
            'Content-Type: text/plain',
            "Authorization: {$aConfEnvio['token']}",
        );
        // $path é o caminho para o arquivo que gerei. Caso precise posso 
        // postar o conteúdo
        $args['file_contents'] = file_get_contents($path);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST,true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec ($ch);
        $info = curl_getinfo($ch);
        $erro = curl_errno($ch);
        curl_close($ch);


        var_dump($result);
        var_dump($info);
        var_dump($erro);
        exit();

Follow execution return:

string(1393) "HTTP/1.1 100 Continue

HTTP/1.1 400 Bad Request
Date: Thu, 04 Jan 2018 11:52:25 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 1106
Connection: keep-alive
X-Powered-By: Express
Access-Control-Allow-Origin: *
X-Content-Type-Options: nosniff
Vary: Accept-Encoding

SyntaxError: Unexpected token -
    at parse (/app/node_modules/body-parser/lib/types/json.js:83:15)
    at /app/node_modules/body-parser/lib/read.js:116:18
    at invokeCallback (/app/node_modules/raw-body/index.js:262:16)
    at done (/app/node_modules/raw-body/index.js:251:7)
    at IncomingMessage.onEnd (/app/node_modules/raw-body/index.js:307:7)
    at emitNone (events.js:106:13)
    at IncomingMessage.emit (events.js:208:7)
    at IncomingMessage.wrapped (/app/node_modules/newrelic/lib/transaction/tracer/index.js:183:28)
    at IncomingMessage.wrappedEmit [as emit] (/app/node_modules/newrelic/lib/transaction/tracer/index.js:220:46)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
    at process.wrappedFunction (/app/node_modules/newrelic/lib/transaction/tracer/index.js:284:51)
"
array(26) {
  ["url"]=>
  string(59) "https://integration-staging.meliuz.com.br/base2/superrocha/"
  ["content_type"]=>
  string(24) "text/html; charset=utf-8"
  ["http_code"]=>
  int(400)
  ["header_size"]=>
  int(287)
  ["request_size"]=>
  int(265)
  ["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(19)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(1.329416)
  ["namelookup_time"]=>
  float(0.509034)
  ["connect_time"]=>
  float(0.50954)
  ["pretransfer_time"]=>
  float(0.609594)
  ["size_upload"]=>
  float(688)
  ["size_download"]=>
  float(1106)
  ["speed_download"]=>
  float(831)
  ["speed_upload"]=>
  float(517)
  ["download_content_length"]=>
  float(1106)
  ["upload_content_length"]=>
  float(688)
  ["starttransfer_time"]=>
  float(1.303348)
  ["redirect_time"]=>
  float(0)
  ["redirect_url"]=>
  string(0) ""
  ["primary_ip"]=>
  string(12) "54.94.158.77"
  ["certinfo"]=>
  array(0) {
  }
  ["primary_port"]=>
  int(443)
  ["local_ip"]=>
  string(12) "192.168.0.85"
  ["local_port"]=>
  int(37200)
}

int(0)


Here comes a mystery ... the problem is not in their WS because when I test the PHPStorm Rest Client it works. Follow print:

Request Answer

The return on the response is already validated there in their WS. Anyone have any tips? I am open to suggestions for improvements or other ways of doing this. Just remembering that it has to be PHP .

Thanks, guys.

    
asked by anonymous 04.01.2018 / 13:10

1 answer

0

By error:

  

SyntaxError: Unexpected token -       at parse (/app/node_modules/body-parser/lib/types/json.js:83:15)

I assume the problem is when using the JSON.parse() command that should be used to convert a string containing JSON notation to a objeto fault JavaScript.

I would debug at this point to see how the variable trying to be converted to JSON is being called.

I hope this can help you.

    
04.01.2018 / 13:22