M
M
Maxim Medvedev2020-01-27 20:59:39
PHP
Maxim Medvedev, 2020-01-27 20:59:39

Null character in a query, how to find it?

Hello, I have the following code:

$data = [
            "date_from" => $date_from,
            "date_to" => $date_to,
            "from" => [
                "extension" => "",
                "number" => ""
            ],
            "to" => [
                "extension" => "",
                "number" => ""
            ],
            "fields" => "records, start, finish, from_extension, from_number, to_extension, to_number, disconnect_reason",
        ];

$json = json_encode($data);
        $sign = hash('sha256', $api_key . $json . $api_salt);
        $post = [
            "vpbx_api_key" => $api_key,
            "sign" => $sign,
            "json" => $json
        ];

 $post = http_build_query($post);

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
        $response = json_decode(curl_exec($ch));
        curl_close($ch);


I'm trying to do the following: there is a telephony api, there is an api key and there is a salt for signatures, I'm trying to send a request to receive data. In response, I receive the message "You are requesting with an invalid credential" via api, clarified in the support service, received a response that there is a null character in the request and they receive the following data on the server side:
"vpbx_api_key": "yiy0wyrvhft6nx4n1qwqfwtmsm4auvwj"
"amp ;sign": ""
"amp;json": ""

I checked it myself, http_build_query() returns vpbx_api_key=[cut]&sign=[cut]&json=[cut]. That is, there is nothing outside. I have already tried changing the file encoding, saving with and without bom, typing all the code again, checking the number of characters in the keys with an array, checking the data using var_dump (), checked it for php7.1-7.3, rewrote the code using a ready-made library from github (there is that same error).

Who can suggest how to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-01-28
@SilenceOfWinter

$post = http_build_query($post); similar conversions have been around for 5 years as "legacy" may need to transfer headers for proper transmission.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question