php8.5
Home/ Manual/ curl / functions/ curl_getinfo

curl_getinfo

PHP function Edit on GitHub ✎

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

Get information regarding a specific transfer

Description

curl_getinfo(CurlHandle $handle, int|null $option = null): mixed

Gets information about the last transfer.

Parameters

option

One of the CURLINFO_* constants.

Return Values

If option is given, returns its value. Otherwise, returns an associative array with the following elements (which correspond to option), or false on failure:

  • "url"
  • "content_type"
  • "http_code"
  • "header_size"
  • "request_size"
  • "filetime"
  • "ssl_verify_result"
  • "redirect_count"
  • "total_time"
  • "namelookup_time"
  • "connect_time"
  • "pretransfer_time"
  • "size_upload"
  • "size_download"
  • "speed_download"
  • "speed_upload"
  • "download_content_length"
  • "upload_content_length"
  • "starttransfer_time"
  • "redirect_time"
  • "certinfo"
  • "primary_ip"
  • "primary_port"
  • "local_ip"
  • "local_port"
  • "redirect_url"
  • "request_header" (This is only set if the CURLINFO_HEADER_OUT is set by a previous call to curl_setopt())
  • "posttransfer_time_us" (Available as of PHP 8.4.0 and cURL 8.10.0)

Note that private data is not included in the associative array and must be retrieved individually with the CURLINFO_PRIVATE option.

Changelog

VersionDescription
8.4.0Introduced CURLINFO_POSTTRANSFER_TIME_T constant and posttransfer_time_us (Curl 8.10.0 or later).
8.3.0Introduced CURLINFO_CAINFO and CURLINFO_CAPATH.
8.2.0Introduced CURLINFO_PROXY_ERROR, CURLINFO_REFERER, CURLINFO_RETRY_AFTER.
8.0.0option is nullable now; previously, the default was 0.
7.3.0Introduced CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, CURLINFO_CONTENT_LENGTH_UPLOAD_T, CURLINFO_HTTP_VERSION, CURLINFO_PROTOCOL, CURLINFO_PROXY_SSL_VERIFYRESULT, CURLINFO_SCHEME, CURLINFO_SIZE_DOWNLOAD_T, CURLINFO_SIZE_UPLOAD_T, CURLINFO_SPEED_DOWNLOAD_T, CURLINFO_SPEED_UPLOAD_T, CURLINFO_APPCONNECT_TIME_T, CURLINFO_CONNECT_TIME_T, CURLINFO_FILETIME_T, CURLINFO_NAMELOOKUP_TIME_T, CURLINFO_PRETRANSFER_TIME_T, CURLINFO_REDIRECT_TIME_T, CURLINFO_STARTTRANSFER_TIME_T, CURLINFO_TOTAL_TIME_T.

Examples

curl_getinfo() example

php
<?php
// Create a cURL handle
$ch = curl_init('http://www.example.com/');

// Execute
curl_exec($ch);

// Check if any error occurred
if (!curl_errno($ch)) {
  $info = curl_getinfo($ch);
  echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
}
?>

curl_getinfo() example with option parameter

php
<?php
// Create a cURL handle
$ch = curl_init('http://www.example.com/');

// Execute
curl_exec($ch);

// Check HTTP status code
if (!curl_errno($ch)) {
  switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
    case 200:  # OK
      break;
    default:
      echo 'Unexpected HTTP code: ', $http_code, "\n";
  }
}
?>

Notes

Note

Information gathered by this function is kept if the handle is re-used. This means that unless a statistic is overridden internally by this function, the previous info is returned.

Source: reference/curl/functions/curl-getinfo.xml · from the official PHP manual (php/doc-en)