CURL PHP request for a WEB Service

Asked

Viewed 593 times

1

Good morning to you all. Guys, I’m having a problem communicating with the web service of a third-party company here where I work. Explaining above, I need to send a report of transactions with coupons that have been identified with the option of Cashback and send via WS to this company to account and generate the credit in the customers account. File generation and everything else is ready, the problem is in uploading the file to WS.

Follow my code and send 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 return of execution:

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 for Phpstorm Rest Client it works. Follows print:

Request Request Answer Resposta

The return on the answer is already from the validation back at their WS. Anybody got any tips? I am open to suggestions for improvements or other ways to do this. Remembering only that it has to be on PHP.

Thanks in advance guys.

1 answer

0

By mistake:

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

I assume the problem is using the command JSON.parse() which should be used to convert a stringcontaining JSON notation in a objeto Javascript.

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

I hope this can help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.