GuzzleHttp\Exception\RequestException::getResponseBodySummary PHP Méthode

getResponseBodySummary() public static méthode

Will return null if the response is not printable.
public static getResponseBodySummary ( Psr\Http\Message\ResponseInterface $response ) : string | null
$response Psr\Http\Message\ResponseInterface
Résultat string | null
    public static function getResponseBodySummary(ResponseInterface $response)
    {
        $body = $response->getBody();
        if (!$body->isSeekable()) {
            return null;
        }
        $size = $body->getSize();
        if ($size === 0) {
            return null;
        }
        $summary = $body->read(120);
        $body->rewind();
        if ($size > 120) {
            $summary .= ' (truncated...)';
        }
        // Matches any printable character, including unicode characters:
        // letters, marks, numbers, punctuation, spacing, and separators.
        if (preg_match('/[^\\pL\\pM\\pN\\pP\\pS\\pZ\\n\\r\\t]/', $summary)) {
            return null;
        }
        return $summary;
    }

Usage Example

Exemple #1
1
 /**
  * Get a response summary (useful for exceptions).
  * Use Guzzle method if available (Guzzle 6.1.1+).
  *
  * @param ResponseInterface $response
  *
  * @return string
  */
 public static function getResponseBodySummary(ResponseInterface $response)
 {
     if (method_exists(RequestException::class, 'getResponseBodySummary')) {
         return RequestException::getResponseBodySummary($response);
     }
     $body = \GuzzleHttp\Psr7\copy_to_string($response->getBody());
     if (strlen($body) > 120) {
         return substr($body, 0, 120) . ' (truncated...)';
     }
     return $body;
 }