1
1
1mb2018-08-22 13:58:21
PHP
1mb, 2018-08-22 13:58:21

Encoding when passing a cURL GET request?

When sending a GET request using cURL on a local server (XAMPP), everything works fine. But as soon as the code is uploaded to a remote server (nginx), the response is not what is expected. I send the string encoded using urlencode() . There is a suspicion that something with the encoding. Anyone in the know please help me figure out what's going on.

private function request($url = '/', $aPost = null, $aOptions = array())
    {
        $ch = curl_init($url);

        curl_setopt_array($ch, [
            CURLOPT_HEADER            => true,
            CURLOPT_RETURNTRANSFER    => true,
            CURLOPT_ENCODING          => '',
            CURLOPT_CONNECTTIMEOUT    => 30,
            CURLOPT_COOKIEJAR         => $this->file_cookies,
            CURLOPT_COOKIEFILE        => $this->file_cookies,
            CURLOPT_USERAGENT         => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36',
            CURLOPT_HTTPHEADER        => [
                'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Content-Type' => 'application/x-www-form-urlencoded',
            ]
        ]);

        if (!is_null($aPost)) {
            curl_setopt_array($ch, [
                CURLOPT_POST         => true,
                CURLOPT_POSTFIELDS   => is_array($aPost) ? http_build_query($aPost) : $aPost
            ]);
        }

        if (count($aOptions)) {
            curl_setopt_array($ch, $aOptions);
        }

        $response = curl_exec($ch);

        if ($response === false || curl_errno($ch) != 0) {
            throw new \Exception(curl_error($ch), curl_errno($ch));
        }
        curl_close($ch);

        return iconv('windows-1251', 'utf-8', $response);
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question