I am writing a php script with a curl request to api in order to get the shipping cost.

Here is the function:

function methodCurl($to,$weight,$price) { $baseUrl = "http://api.postcalc.ru/?"; $baseUrl .= $this->indexFrom; if(!empty($to)){ $to = rawurlencode ($to); $baseUrl .= "&t=".$to; } if(!empty($weight)){ $baseUrl .= "&w=".$weight; } $baseUrl .= $this->country; $baseUrl .= "&o=php"; echo $baseUrl . "<br>"; $ch = curl_init($baseUrl); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0); curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']); $result = curl_exec($ch); $info = curl_getinfo($ch); $err = curl_error($ch); $errmsg = curl_errno($ch); $info['result'] = $result; $info['error'] = $err; $info['errormsg'] = $errmsg; if($info['http_code'] == 200){ echo mb_detect_encoding($info['result']) . "</br>"; echo "<pre>"; print_r($info); echo "</pre>"; } } 

And everything seems to be good, the request is formed correctly (if the received request is inserted into the browser, everything opens normally), but when outputting the result through a script, the crap is displayed:

enter image description here

Encoding checked everywhere - utf-8. What could be the problem?

  • I do not understand what echo mb_detect_encoding ($ info ['result']) displays. "</ br>"; ? - root Povierennyy
  • This is a coding check, displays UTF-8 - Grekov Ilya
  • Show headlines. Perhaps compressed text is forcibly arriving, even though you have not requested it. But since the browser perceives normally, this should be stated in the response headers. - Fine
  • HTTP / 1.1 200 OK Server: nginx Date: Mon, 04 Jul 2016 08:59:34 GMT Content-Type: text / html; charset = utf-8 Connection: close X-Powered-By: PHP / 5.4.4-14 + deb7u2 Content-Encoding: gzip PostcalcServer: 88.198.243.244 X-Cache-Status: MISS - Grekov Ilya

1 answer 1

I guess. Read carefully the documentation, the link to which you yourself gave in the question:

Postcalc.RU server response is always archived in gzip

And a couple of sentences further described how to live with it further and even described several possible rakes

 // Добавляем распаковку: if ( substr($Response,0,3) == "\x1f\x8b\x08" ) $Response=gzinflate(substr($Response,10,-8));