---
title: newrelic_get_trace_metadata
source: https://docs.newrelic.com/docs/apm/agents/php-agent/php-agent-api/newrelicgettracemetadata
---

## Syntax

```php
newrelic_get_trace_metadata()
```

Returns an associative array containing the identifiers of the current trace and the parent span.

## Requirements

Requires PHP agent [version 9.3 or higher](https://docs.newrelic.com/docs/release-notes/agent-release-notes/php-release-notes).

Must be called inside a [transaction](https://docs.newrelic.com/docs/accounts-partnerships/education/getting-started-new-relic/glossary#transaction).

## Description

Returns an associative array containing the identifiers of the current trace and the parent span. This information is useful for integrating with third party distributed tracing tools, such as Zipkin.

## Return values

An associative array containing the keys:

-   `trace_id`: the currently executing trace identifier. An empty value will be returned if the transaction does not support this functionality or distributed tracing is disabled.Returns:
-   `span_id`: the currently executing span identifier. An empty value will be returned if the transaction does not support this functionality or distributed tracing is disabled.

## Examples

### Populate B3 Headers for use with Zipkin

Adds necessary distributed tracing metadata to the HTTP headers being sent to a Zipkin consumer:

```php
function make_http_request($url) {
    $metadata = newrelic_get_trace_metadata();
    $sampled = newrelic_is_sampled();

    $ch = curl_init($url);

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-B3-TraceId: ' . $metadata['trace_id'],
        'X-B3-SpanId: ' . substr(uniqid() . uniqid(), 0, 16),
        'X-B3-ParentSpanId: ' . $metadata['span_id'],
        'X-B3-Sampled: ' . $sampled));

    return curl_exec($ch);
}

$status = make_http_request("zipkin-consumer.example");
```
