Search code examples
phpapiguzzlecyrillic

Why Cyrillic characters are not handled properly when sending requests using Pool of Guzzle PHP library?


I am using Guzzle to send the requests to the external API end-point.

In my request there is a text query value - 'https://api_endpoint/' . '?text=' .$text

When I am sending the requests one by one:

$response = $client->request(
            'GET',
            ''https://api_endpoint/' . '?text=' .$text,
        );

it works fine for any language in the text field. However, when I first iterate another query value and create an array of the requests and then send these requests using Pool:

$responses = Pool::batch($client, $requests, array(
        'concurrency' => 15,
    ));

In this case I am getting a “bad request” from the API end-point, if the text field in the request was written in Cyrillic. If, however the text field is in latin characters, everything works fine. Same for the situation when I am sending the requests one by one.

I assume that there is some problem with encoding when Guzzle Pool is used.

How do I fix it or workaround this issue?


Solution

  • It's not a Guzzle's issue. You have to urlencode $text before concatenating. Do urlencode($text) or use Guzzle's query option:

    $response = $client->request(
        'GET',
        'https://api_endpoint/',
        ['query' => ['text' => $text]],
    );